Professional Documents
Culture Documents
1. Linear Search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
Output:-
2. Binary Search:-
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Output:-
EXPERIMENT -2
1. Insertion Sort:-
#include <stdio.h>
EXPERIMENT -2
1.Insertion Sort:-
int main()
{
int n, array[1000], c, d, t, flag = 0;
return 0;
}
Output:-
2.Bubble Sort:-
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
return 0;
}
3.Selection Sort:-
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
return 0;
}
Output:-
EXPERIMENT -3
1.Merge Sort:-
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
#include <stdio.h>
int main()
{
int arr[10], no, i, j, c, heap_root, temp;
printf("Input number of elements: ");
scanf("%d", &no);
printf("\nInput array values one by one : ");
for (i = 0; i < no; i++)
scanf("%d", &arr[i]);
for (i = 1; i < no; i++)
{
c = i;
do
{
heap_root = (c - 1) / 2;
/* to create MAX arr array */
if (arr[heap_root] < arr[c])
{
temp = arr[heap_root];
arr[heap_root] = arr[c];
arr[c] = temp;
}
c = heap_root;
} while (c != 0);
}
printf("Heap array : ");
for (i = 0; i < no; i++)
printf("%d\t ", arr[i]);
for (j = no - 1; j >= 0; j--)
{
temp = arr[0];
arr[0] = arr[j];
arr[j] = temp;
heap_root = 0;
do
{
c = 2 * heap_root + 1;
if ((arr[c] < arr[c + 1]) && c < j-1)
c++;
if (arr[heap_root]<arr[c] && c<j)
{ Output:-
temp = arr[heap_root];
arr[heap_root] = arr[c];
arr[c] = temp;
}
heap_root = c;
} while (c < j);
}
printf("\nSorted array : ");
for (i = 0; i < no; i++)
printf("\t%d", arr[i]);
printf("\n"); }