You are on page 1of 19

1:- Binary search #include <stdio.h> #include <conio.

h> void main () { int a [20], first, last, middle, n, i, item; clrscr (); printf ( "Enter the number of elements\n" ); scanf ( "%d", &n ); for ( i = 0; i < n; i++ ) { printf ( "Enter element %d:", i+1); scanf ( "%d", &a[i] ); } printf ("Enter the element to be searched\n"); scanf ( "%d", &item ); first last middle = = = 0; n-1; ( first + last ) / 2;

while ( item != a[middle] && first <= last ) { if ( item > a[middle] ) first = middle + 1; else last = middle - 1; middle = ( first + last ) / 2; }

if ( item == a[middle] ) printf ( "\n %d found at position %d \n ", item, middle + 1 ); if ( first > last ) printf ( "%d not found in array \n", item ); getch ( ); } Output of Binary Search :-

2:- Bubble Sort #include<stdio.h> #include<conio.h> void bubble_sort ( int a[ ], int n ); void main() { int i, j, a[ 20 ], n, temp; clrscr ( ); printf ( "\n Enter the number of elements \n" ); scanf ( "%d", &n ); printf( " Enter the array elements\n " ); for ( i = 0; i < n; i++ ) scanf ( "%d", &a[ i ] ); bubble_sort ( a, n ); printf ( "\n The sorted elements are\n " ); for ( i = 0; i < n; i++ ) printf ( " %d\n ", a[ i ] ); getch( ); } void bubble_sort ( int a[ ], int n ) { intpass, temp, j; for ( pass = 1 ; pass < n; pass++ ) { for ( j = 0; j <= n; j++ ) { if ( a[ j ] > a[ j+1] ) { temp = a [ j ]; a[ j ] = a[ j+1 ]; a[ j+1] = temp; }

} } }

Output of Bubble sort program :-

3:- Heap Sort #include<stdio.h> #include<conio.h> void heapfy ( int a[ ], int n ); int adjust ( int a[ ], int n ); int heaps ( int a[ ], int n);

void heapfy ( int a[ ], int n ) { int x, y, z, it; for ( z = 2; z <= n; z++ ) { it = a[ z ] ; x = z; y = x / 2;

while ( y != 0 && it > a[y] ) { a[ x ] = a[ y ]; x = y; y = x/2; } a[ x ] = it; } }

adjust ( int a[ ], int n ) { int it, x, y; y =1; it = a[ y ]; x = 2 * y;

while ( x < n ) { if ( x+1 < n ) { if ( a[ x ] < a[ x+1 ] ) x++; } if ( it < a[ x ] ) { a[ y] = a[ x ]; y = x; x = 2 * y; } else break; } a[ y ] = it; }

heaps ( int a[ ], int n ) { int i, temp;

heapfy ( a, n);

for ( i = n; i >= 1; i-- ) { temp = a[ 1 ]; a[ 1 ] = a[ i ]; a[ i ] = temp; adjust ( a, i ); } } void main() { int i, n, a[ 100 ]; clrscr ( );

printf ( "enter the number of elements" ); scanf ( "%d", &n ); printf ( "enter the array elements" ); for ( i =1; i <= n; i++) scanf ( "%d", &a[ i ] );

heaps ( a, n );

printf ( "the sorted elements are\n" ); for ( i =1; i <= n; i++ ) printf ( "%d\n", a[ i ] );

getch( ); }

Output of heap sort program :-

4:- Quick Sort #include<stdio.h> #include<conio.h>

void main() { int i, n, a[ 20 ]; float f; clrscr( );

printf ( " enter the size of array " ); scanf ( "%d", &n );

printf ( "\n enter the array elements\n" ); for ( i = 0; i < n; i++ ) scanf ( "%d", &a[ i ] ); quicksort ( a, 0, n-1);

printf ( "the sorted elements are " ); for ( i = 0; i < n; i++ ) printf ( "\n%d", a[ i ] );

getch( ); }

int quicksort ( int a[ ], int low, int high ) { int j; if ( low < high ) { j = partition ( a, low, high ); quicksort ( a, low, j-1 );

quicksort ( a, j +1, high ); } }

int partition ( int a[ ], int low, int high ) { int i, j, temp, key; key = a[ low ]; i = low + 1; j = high;

while ( 1 ) { while ( i < high && key > a[ i ] ) i++; while ( key <a[ j ] ) j--;

if ( i < j ) { temp = a[ i ]; a[ i ] = a[ j ]; a[ j ] = temp; } else { temp = a[ low ]; a[ low ] = a[ j ]; a[ j ] = temp; return j; } } } Output of Quick Sort program :-

4:- Merge sort #include<stdio.h> #include<conio.h>

void main( ) { int i, a[ 100 ], n; clrscr( );

printf ( "enter the size of array" ); scanf ( "%d", &n );

printf ( "enter the array elements\n" ); for( i = 0; i < n; i++ ) scanf ( "%d", &a[ i ] );

merge_sort ( a, 0, n-1);

printf ( "\nthe sorted elements are\n" ); for ( i = 0; i < n ; i++ ) printf ( "%d\n", a[ i ] ); getch( ); }

int merge_sort ( int a[ ], int low, int high ) { int mid; if ( low < high ) { mid = ( low + high ) / 2; merge_sort ( a, low, mid ); merge_sort ( a, mid +1, high ); merge ( a, low, mid, high ); } }

int merge ( int a[ ], int low, int mid, int high ) { int i, j, k, res[ 30 ]; i = low; j = mid + 1; k = low; while ( ( i <= mid ) && ( j <= high ) ) { if ( a[ i ] < a[ j ] ) { res[ k ] = a[ i ]; k = k + 1; i = i + 1; }

else { res[ k ] = a[ j ]; k = k + 1; j = j + 1; } } while ( i <= mid ) { res[ k ] = a[ i ]; k = k + 1; i = i + 1; } while ( j <= high ) { res[ k ] = a[ j ]; k = k + 1; j = j + 1; } for ( i =0; i <= k-1; i++ ) a[ i ] = res[ i ]; }

Output of Merge Sort program :-

5:- Stack Operation #include<stdio.h> #include<conio.h> #define maxstk 5 int top = -1; int s [ maxstk ]; void main( ) { int ch; clrscr( ); while ( 1 ) { printf ( "\n 1: push \n 2: pop \n 3:display \n 4: exit \n Enter your choice\n" ); scanf ( "%d", &ch ); switch ( ch ) { case 1:

push(); break; pop(); break; display(); break; exit(1); printf("Wrong choice\n");

case 2:

case 3:

case 4: default : } } }

push ( ) { int item; if ( top == ( maxstk - 1) ) printf ( "stack overflow \n" ); else { printf ( "Enter the item to be pushed in stack\n" ); scanf ( "%d", &item ); top++; s[ top ] = item; } } pop ( ) { if ( top == -1) printf ( "stack underflow\n" ); else { printf ( "popped element is %d", s[ top ] ); top--; } } display( ) { int i; if ( top == -1) printf ( "stack is empty \n" ); else { printf ( "stack elements are :\n" ); for ( i = top; i >= 0; i--) printf ( "%d", s[ i ] ); } }

Output of Stack program :-

You might also like