Lab Report 2 - 3 (232-15-902)
Lab Report 2 - 3 (232-15-902)
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
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.
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
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 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.
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..