Professional Documents
Culture Documents
1. Linear Search
#include <stdio.h>
int main(void)
{
int a[10],n,i,se,count=0;
printf("Enter no of element into an array\n");
scanf("%d",&n);
printf("Enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter search element");
scanf("%d",&se);
for(i=0;i<n;i++)
{
if(a[i]==se)
{
printf("\n%dElement found=%d",a[i],se);
count=count+1;
break;
}
}
if(count==0)
{
printf("%d Element not Found:",se);
}
return 0;
}
Output: Element Found
Enter no of element into an array
5
Enter array elements
89
74
15
56
2
Enter search element56
56 Element found=56
2. Binary Search
#include <stdio.h>
int main(void)
{
int i, start, end, mid, n, search, a[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i=0;i<n;i++)
scanf("%d", &a[i]);
printf("Enter value to find\n");
scanf("%d",&search);
start=0;
end=n-1;
mid=(start+end)/2;
while(start<= end)
{
mid=(start+end)/2;
if (array[mid]==search)
{
printf("%d found at location %d.\n", search, a[mid]);
break;
}
else if (a[mid]<search)
{
start = mid+1;
}
else
{
end = mid-1;
}
}
if (start > end)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
3. Insertion Sort
#include <stdio.h>
int main(void)
{
int a[100],n,i,key,j;
printf("Enter no of elements");
scanf("%d",&n);
printf("Enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (i = 1; i < n; i++)
{
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
printf("Sorted list is:");
for (i = 0; i < n; i++)
{
printf("\n%d ", a[i]);
printf("\n");
}
return 0;
}
Output:
Enter no of elements
5
Enter array elements
48
5
79
3
41
Sorted list is:
3
41
48
79
4. Selection Sort
#include <stdio.h>
int main(void) {
int a[10],n,i,j,pos,temp;
printf("Enter no of elements");
scanf("%d",&n);
printf("Enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (i = 0; i < (n - 1); i++)
{
pos = i;
for (j = i + 1; j < n; j++) {
if (a[pos] > a[j])
pos = j;
}
if (pos != i)
{
temp = a[i];
a[i] = a[pos];
a[pos] = temp;
}
}
printf("Sorted list is:\n\n");
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
return 0;
}
Output:
Enter no of elements
5
Enter array elements
78
6
45
3
2
Sorted list is:
2 3 6 45 78
5. Bubble Sort
#include <stdio.h>
int main(void)
{
int a[100],n,i,j,temp;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(i= 0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;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;
}
}
}
Output:
Enter number of elements
6
Enter 6 integers
89
5
56
3
47
52
Sorted list in ascending order:
3
5
47
52
56
89
6. Quick Sort
#include <stdio.h>
int main(){
int i,n,a[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&n);
printf("Enter %d elements: ", n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("Order of Sorted elements: ");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
return 0;
}
quicksort(int a[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j)
{
while(a[i]<=a[pivot]&&i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
Output:
How many elements are u going to enter?: 6
Enter 6 elements:
89
2
78
3
45
6
Order of Sorted elements:
2
3
6
45
78
89
7. Merge Sort
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[100],n,i;
printf("enter the no of elements");
scanf("%d",&n);
printf("Eneter the elements into an array");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int arr_size =n;
Sorted array is
2 3 4 45 56 89