0% found this document useful (0 votes)
36 views

Searching and Sorting

The document contains C programs for implementing various sorting and searching algorithms: 1) Linear search to search an item in an array. 2) Binary search to search a sorted array more efficiently than linear search. 3) Bubble, selection, and insertion sort algorithms to sort arrays in ascending order. 4) Counting sort, a non-comparison sorting algorithm that works on integer data.

Uploaded by

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

Searching and Sorting

The document contains C programs for implementing various sorting and searching algorithms: 1) Linear search to search an item in an array. 2) Binary search to search a sorted array more efficiently than linear search. 3) Bubble, selection, and insertion sort algorithms to sort arrays in ascending order. 4) Counting sort, a non-comparison sorting algorithm that works on integer data.

Uploaded by

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

C Programs for searching and sorting

//program for linear search


#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int item,i,loc =-1;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

printf("\nEnter an item to be searched in the array\n");


scanf("%d",&item);

for(i=0;i<N;i++)
{
if(arr[i]==item)
{
loc = i;
printf("\nItem found at location = %d\n",loc);
break;
}
}

if(loc==-1)
{
printf("\nItem not found\n");
}
return(0);
}
//program for binary search
#include<stdio.h>

#define N 10

int main()
{
int arr[N];
int item,loc =-1;
int m,i, f = 0, l = N-1;

printf("Enter an sorted array in ascending order of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

printf("\nEnter an item to be searched in the array\n");


scanf("%d",&item);
while(f<=l)
{
m = (f+l)/2;

if(arr[m]==item)
{
loc = m;
printf("\nItem found at location = %d\n",loc);
break;
}
else if(item<arr[m])
{
l = m - 1;
}
else
{
f = m + 1;
}
}
if(loc==-1)
{
printf("\nItem not found\n");
}
return(0);
}
//program for bubble sort
#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,temp;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("\nSorted array after bubble sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for selection sort
#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,temp,min;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<N-1;i++)
{
min = i;

for(j=i+1;j<N;j++)
{
if(arr[j]<arr[min])
{
min = j;
}
}
if(min!=i)
{
temp = arr[i];
arr[i] = arr[min]; //swapping minimum element with element at index i
arr[min] = temp;
}
}

printf("\nSorted array after selection sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for insertion sort

#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,key;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

for(i=1;i<N;i++)
{
key = arr[i];
j = i-1;
while(j>=0 && key<arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}

arr[j+1]=key;
}

printf("\nSorted array after insertion sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for counting sort
#include<stdio.h>

#define N 10

int main()
{
int arr[N];
int k,i;
int b[N]; //b is output array

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

k = arr[0];

for(i = 1; i<N; i++) //finding maximum value k = range = [0...k] = max value among the given array numbers
{
if(arr[i]>k)
{
k = arr[i];
}
}

int count[k+1]; //create count array of size k+1 range = [0...k]

//for turbo compiler create dynamically like int * count = (int *) malloc((k+1)*sizeof(int));

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


{
count[i] = 0; // Initialize count array with all zeros
}

for(i=0;i<N;i++)
{
count[arr[i]]++; //count array now contains frequencies of all elements
}

for(i=1;i<=k;i++)
{
count[i] = count[i] + count[i-1]; //update count array// cumulitive frequencies
}
for(i=N-1;i>=0;i--) //create an output array
{
b[--count[arr[i]]] = arr[i];
}

for(i = 0; i<N; i++) //copy output array to original array


{
arr[i] = b[i];
}

printf("\nSorted array after counting sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}

You might also like