Data Structures Lab WEEK - 10: 1.write A C Program To Implement Linear Search Using Non-Recursion Functions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

DATA STRUCTURES LAB

WEEK -10

Name : B.MAHESH
Roll No : 20R21A05D3
Date : 18/12/2021

PROBLEM STATEMENT:

1.Write a c program to implement Linear search using non-


recursion functions.
CODE:

#include<stdio.h>
int lsearch(int a[],int n,int key);
int a[50],n,key;
int main()
{
int i,res;
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element : ");
scanf("%d",&key);
res=lsearch(a,n,key);
if(res==-1)
printf("Element not found\n");
else
printf("Element found at %d location",res);
}
int lsearch(int a[],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(key==a[i])
return i;
}
return -1;
}
OUTPUT:

2. Write a c program to implement Linear search using Recursion


functions.

CODE:
#include<stdio.h>
int Linear_search(int arr[], int Search_ele, int n)
{
int i;
static int temp=0;
if(n>0)
{
i=n-1;
if(arr[i]==Search_ele)
temp=1;
Linear_search(arr,Search_ele,i);
}
return temp;
}
int main()
{
int n,j,temp;
printf("Enter your array size:");
scanf("%d",&n);
int arr[n];
printf("Enter the Array Element:");
for(j=0;j<n;j++)
{
scanf("%d",&arr[j]);
}
int Search_ele;
printf("Enter the search element:");
scanf("%d",&Search_ele);
if(Linear_search(arr,Search_ele,n)==1)
printf("Element found.... %d");
else
printf("Element not found....");
return 0;
}

OUTPUT:

TIME COMPLEXITY:
Best case:O(1)
Average case: O(n)
Worst case: O(n/2)
3. Write a c program to implement Binary search using non-recursion
functions.
A. CODE:
#include<stdio.h>
int bsearch(int a[],int n,int key);
int a[50],n,key;
int main()
{
int i,res;
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element : ");
scanf("%d",&key);
res=bsearch(a,n,key);
if(res==-1)
printf("Element not found\n");
else
printf("Element found at %d location",res);
}
int bsearch(int a[],int n,int key)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
return mid;
else if(key<mid)
high=mid-1;
else
low=mid+1;
}
return -1;
}
OUTPUT:
4.Write a c program to implement Binary search using Recursion
functions.
A. OUTPUT:
/*
* C Program to Perform Binary Search using Recursion
*/
#include <stdio.h>
void binary_search(int [], int, int, int);

int main()
{
int key, size, i;
int list[25];
printf("Enter size of a list: ");
scanf("%d", &size);
printf("Enter elements\n");
for(i = 0; i < size; i++)
{
scanf("%d",&list[i]);
}

printf("\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);
}

void binary_search(int list[], int lo, int hi, int key)


{
int mid;
if (lo > hi)
{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}
}
OUTPUT:

TIME COMPLEXITY:
Best case:O(1)
Average case: O(2 log N)
Worst case: O(2 log N)
5. write a C program to implement bubble sort using functions.
A. OUTPUT:
//program on the bubble sort//
#include<stdio.h>
void bubble_sort(int a[],int n);
int main()
{
int a[20],n,i;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Before sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
bubble_sort(a,n);
return 0;

}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:

TIME COMPLEXITY:
Best case:O(n*n)
Worst case: O(n*n)
Average case: O(n*n)

6. write a C program to implement Insertion sort using functions.

A. CODE:

//program on the insertion sort//


#include<stdio.h>

void insert_sort(int a[],int n);

int main()

int a[20],n,i;

printf("Enter the size of the array\n");

scanf("%d",&n);

printf("Enter the array elements\n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Before sorting\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

insert_sort(a,n);
return 0;

void insert_sort(int a[],int n)

int i,j,temp;

for(i=1;i<n;i++)

temp=a[i];

j=i-1;

while((temp<a[j]&&j>=0))

a[j+1]=a[j];

j=j-1;

a[j+1]=temp;

printf("After sorting\n");

for(i=0;i<n;i++)

{
printf("%d\t",a[i]);

CODE:

TIME COMPLEXITY:
Best case:O(n)
Worst case: O(n*n)
Average case: O(n*n)
7.write a C program to implement selection sort using functions.
A. CODE:
//program on the selection sort//
#include<stdio.h>
void select_sort(int a[],int n);
int main()
{
int a[20],n,i;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Before sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
select_sort(a,n);
return 0;

}
void select_sort(int a[],int n)
{
int i,j,temp,min;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("After sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:

TIME COMPLEXITY:
Best case:O(n*n)
Worst case: O(n*n)
Average case: O(n*n)

8. write a C program to implement quick sort using functions.


A. CODE:
#include<stdio.h>
void quick_sort(int a[100],int first,int last);
int a[100],n;
int main()
{
int i;
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n The elements before Sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
quick_sort(a,0,n-1);
printf("\n The elements after Sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
void quick_sort(int a[100],int first,int last)
{
int i,j,t,pivot,n,temp;
if(first<last)
{
i=first;
j=last;
pivot=first;
while(i<j)
{
while(a[i]<=a[pivot] && i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
t=a[pivot];
a[pivot]=a[j];
a[j]=t;
quick_sort(a,first,j-1);
quick_sort(a,j+1,last);
}
}
OUTPUT:

TIME COMPLEXITY:
Best case:O(log n)
Worst case: O(log n)
Average case: O(n*n)

9.write a C program to implement Merge sort using functions.


OUTPUT:
#include<stdio.h>
void mergesort(int a[],int low,int high);
void merge(int a[],int low,int high,int mid);
int main()
{
int a[100],i,n;
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n The elements before Sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
mergesort(a,0,n-1);
printf("\n The elements after Sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void mergesort(int a[100],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
}
void merge(int a[],int low,int high,int mid)
{
int i,j,k,c[50];
i=low;
j=mid+1;
k=low;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
c[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
c[k]=a[i];
i++;
k++;
}
while(j<=high)
{
c[k]=a[j];
j++;
k++;
}
for(i=0;i<k;i++)
a[i]=c[i];
}

OUTPUT:

TIME COMPLEXITY:
Best case:O(log n)
Worst case: O(log n)
Average case: O(log n)

You might also like