0% found this document useful (0 votes)
14 views9 pages

Lab Report 2 - 3 (232-15-902)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Lab Report 2 - 3 (232-15-902)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer Science And Engineering (CSE)

Faculty of Science and Information Technology


Semester: Fall Year: 2024

LAB REPORT
Course Title : Algorithm Lab
Course Code : CSE 214
Lab Report : 2 & 3

Submitted to
Jawad Hossain
Lecturer
Department of CSE
Daffodil International University

Submitted by
Nur Mohammed
ID: 232 - 15 -902
Section: 65_C2

Submission Date: 13/09/2024


LAB REPORT – 2

Problem 1: Finding an element in an Unsorted array.


Problem Statement: Executing a linear search algorithm in C programming
language that searches for a given element in an array. The algorithm returns the
index of the element if found or indicate that the element is not present in the
array.
Introduction: Linear searching is a elementary search that scans each element
of the array one by one to find the targeted key.

Source Code:

#include<stdio.h>
int linearsearch(int arr[],int n, int t)
{
for(int i=0;i<n;i++)
{
if(arr[i]==t)
{
printf("%d",i+1);
}
}
}
int main()
{
int arr[]={3,8,7,98,50,16,48,300};
int t;
int n=sizeof(arr)/sizeof(arr[0]);
printf("Give me the value which you want:\n");
scanf("%d",&t);
int ans=linearsearch(arr,n,t);

}
Input/Output :
Input Output
Give me the value which you want:48 7

Discussion:
This C program implements a linear search algorithm, which sequentially checks each
element of an unsorted array to find a given key . It has an advantage is in using in sort
database therefore if we have a sort database we can use linear searching efficiently.

Problem2: Finding an element in a Sorted Array

Problem Statement: Applying a Binary search algorithm in C programming


language that searches for a element in a sorted array. The algorithm returns the
index of the element if found in the sorted array or indicate the element is not
found in the array.

Introduction: With binary search we can efficiently finds a targeted value in a


sorted array by continuously dividing the search interval in half. It significantly
decreases the number of comparisons needed compared to linear search.

Source Code:

#include<stdio.h>
int binarysearch(int arr[],int n,int t)
{
int l=0, r=n-1;
while(l<=r){

int
mid=(l+r)/2;
if(arr[mid]==t)
return mid;
if(arr[mid]<t
) l=mid+1;
else r=mid-
1;
}
}
int main()
{
int arr[]={1,3,5,8,12,19};
int n=sizeof(arr)/sizeof(arr[0]);
int t;
printf("Give me the number you want to search: \n");
scanf("%d",&t);
int ans=binarysearch(arr,n,t);
printf("%d\n",ans+1);
}

Input/Output:

Input Output
Give me the number you want to search: 5
12

Discussion:
Binary search is highly efficient for both sorted and large databases. It is much faster than
linear search, but the key requirement is that the array used must be sorted.

LAB REPORT – 3

Problem1: Sorting an array using Insertion sort


Problem Statement: Applying the insertion sort algorithm in C programming
language to sort an array of numbers in ascending order.
Introduction: Insertion sort builds a sorted portion of the array one element at
a time. It is simple and efficient for small or nearly sorted arrays.

Source code:
#include <stdio.h>
int insertionSort(int arr[], int n) {
int i, k, j;
for (i=1;i<n;i++) {
k=arr[i];
j =i-1;
while(j>=0&&arr[j]>k)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=k;
}
}

int printing(int arr[], int n) {


int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int n;
printf("Size of Array: \n");
scanf("%d",&n);
int arr[n];

printf("Array: ");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);

insertionSort(arr, n);
printing(arr,n);

}
Input/Output:
Input Output
Size of Array: 5 3 4 8 9 48
Array: 4 8 9 48 3

Discussion:
Insertion sort works well for small or nearly sorted datasets and is easy to
implement. It is easy to implement but it is not efficient to use for long or big
database because the time complexity of the algorithm will be order of n square.

Problem2: Sorting an array using Bubble Sort


Problem Statement: Applying Bubble sort algorithm in C programming
language to sort an array of number in ascending order.
Introduction: Bubble sort is a simple sorting algorithm that continuously
compares and swaps adjacent elements if they are in wrong order.

Source Code:
#include<stdio.h>
int bublesort(int arr[], int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
}
void printing(int arr[], int n)
{

for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
int n;
printf("Size of Array: \n");
scanf("%d",&n);
int arr[n];

printf("Array: ");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);

bublesort(arr,n);
printf("Sorting Array =\n");
printing(arr,n);
return 0;
}
Input/Output:

Input Output
User input
Size of Array: 5, Array: 10 8 9 5 100 5 8 9 10 100
Discussion:
Bubble sort is simple but inefficient for large datasets because of its \(O(n^2)\)
time complexity. It works well on small or mostly sorted arrays, but its efficiency
drops significantly as the array size grows..

You might also like