Sorting s

21
Sorting Techniques http://www.iitg.ernet.in/psm/indexing_ma513/y09/index.html /* Bubble sort. */ #include <stdio.h> #include <conio.h> void main( ) { int a[50],n, i, j, temp ; clrscr( ) ; printf ( "How Many Numbers “) ; scanf(“%d”,&n); printf ( "Enter the Elements\n") ; for ( i = 0 ; i <= 4 ; i++ ) scanf(“%d”,&a[i]); for ( i = 0 ; i < n-1 ; i++ ) { for ( j = 0 ; j <= (n - 1) - i ; j++ ) { if ( a[j] > a[j + 1] ) { temp = a[j] ; a[j] = a[j + 1] ; a[j + 1] = temp ; } } } printf ( "Elements fter sorting:\n") ; for ( i = 0 ; i < n-1 ; i++ ) printf ( "%d\t", a[i] ) ; getch( ) ; }

description

All types of Sorting Techniques

Transcript of Sorting s

Page 1: Sorting s

Sorting Techniqueshttp://www.iitg.ernet.in/psm/indexing_ma513/y09/index.html/* Bubble sort. */

#include <stdio.h>#include <conio.h>

void main( ){

int a[50],n, i, j, temp ;clrscr( ) ;

printf ( "How Many Numbers “) ; scanf(“%d”,&n);

printf ( "Enter the Elements\n") ;for ( i = 0 ; i <= 4 ; i++ )

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

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

for ( j = 0 ; j <= (n - 1) - i ; j++ ){

if ( a[j] > a[j + 1] ){

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

}}

}

printf ( "Elements fter sorting:\n") ;

for ( i = 0 ; i < n-1 ; i++ )printf ( "%d\t", a[i] ) ;

getch( ) ;}

Page 2: Sorting s

Sorting Techniques

/* Interchange Sort. */

#include <stdio.h>#include <conio.h>

void main( ){

int a[50],n, i, j, temp ;

clrscr( ) ;

printf ( "How Many Numbers “) ; scanf(“%d”,&n);

printf ( "Enter the Elements\n") ;for ( i = 0 ; i <= 4 ; i++ )

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

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

for ( j = i + 1 ; j < n-1 ; j++ ){

if ( a[i] > a[j] ){

temp = a[i] ;a[i] = a[j] ;a[j] = temp ;

}}

}

printf ( "\n\nArray after sorting:\n") ;

for ( i = 0 ; i < n-1 ; i++ )printf ( "%d\t", a[i] ) ;

getch( ) ;}

Page 3: Sorting s

Sorting Techniques

/* Selection Sort */

#include<stdio.h>

void main() { int a[100], n, i, j, max, temp; clrscr(); printf("How many "); scanf("%d",&n); printf("Enter Numbers \n"); for( i = 0; i < n; i++ ) scanf("%d", &a[i]);

for (i = 0; i <= n-1; i++) { max = i;

for( j = i + 1; j < n - 1; j++) if(a[j] < a[max])

max = j;

temp = a[i]; a[i] = a[max];

a[max] = temp; }

printf("Sorted Numbers \n");

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

getch(); }

Page 4: Sorting s

Sorting Techniques

/* Insertion sort. */

#include <stdio.h>#include <conio.h>

void main( ){

int a[100],n, i, j, k, temp ;

clrscr( ) ;

printf("How many "); scanf("%d",&n); printf("Enter Numbers \n"); for(i=0;i<n;i++) scanf("%d",&a[i]);

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

for ( j = 0 ; j < i ; j++ ){

if ( a[j] > a[i] ){

temp = a[j] ;a[j] = a[i] ;

for ( k = i ; k > j ; k-- )a[k] = a[k - 1] ;

a[k + 1] = temp ;}

}}

printf ( "\n\nArray after sorting:\n") ;

for ( i = 0 ; i <= n-1 ; i++ )printf ( "%d\n", a[i] ) ;

getch( ) ;}

Page 5: Sorting s

Sorting Techniques/* Insertion Sort */

#include<stdio.h>void insertionSort(int a[],int n) { int j, p, temp;;

for( j = p; p < n; p++) {

temp = a[p]; for( j = p; j > 0 && temp < a[ j - 1 ]; j--) a[ j ] = a[ j - 1 ]; a[ j ] = temp;

} }

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

printf("How Many elements : "); scanf("%d",&n);

printf("Enter the Elements \n"); for( i = 0; i < n; i++ )

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

insertionSort( a, n );

printf("Sorted Elements are\n"); for( i = 0; i < n; i++ )

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

}

Page 6: Sorting s

Sorting Techniques/* Quick Sort */

#include<stdio.h>

void quick(int a[],int lb,int ub) { int key, i, j, temp, flag;

if( lb < ub ) { key =a[lb]; i = lb;

j = ub+1; flag = 1;

while( flag == 1 ) {

i++; while( a[i] < key )

i++; j--; while( a[j] > key )

j--; if( i < j ) {

temp = a[i]; a[i] = a[j]; a[j] = temp;

} else flag = 0; }

temp = a[lb]; a[lb] = a[j]; a[j] = temp;

quick(a,lb,j-1); quick(a,j+1,ub);

} }

Page 7: Sorting s

Sorting Techniques

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

clrscr();

printf("How many "); scanf("%d",&n);

printf("Enter Numbers \n"); for( i = 0; i < n; i++ ) scanf("%d",&a[i]); quick(a, 0, n-1); printf("Sorted Numbers \n"); for( i = 0; i < n; i++ ) printf("%d\n",a[i]); }

Page 8: Sorting s

Sorting Techniques

/* Heap Sort. */

#include <stdio.h>#include <conio.h>

void Heap ( int a[ ], int n);void Adjust ( int a[ ], int i, int n) ;

void main( ){

int lp[100], n, i , count;

clrscr( ) ;printf("How many ");scanf("%d",&count);

printf("Enter Numbers \n");for( i = 0; i < count; i++ ) scanf("%d",&lp[i]);

Heap ( lp, ( count - 1 ) ) ;

printf ( "Sorted Elements \n" ) ;for ( i = 0 ; i <= count - 1 ; i++ )

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

getch( );}

void Heap ( int a[ ], int n ) {

int i, temp ;for ( i = n / 2 ; i >= 0 ; i-- ) Adjust( a , i , n);

for ( i = ( n - 1 ) ; i >= 0 ; i-- ) { temp = a[ i + 1 ]; a[ i + 1 ] = a[ 0 ]; a[ 0 ] = temp; Adjust(a , 0 , i); }

}

Page 9: Sorting s

Sorting Techniques

void Adjust ( int a[ ], int i , int n ) {

int j, k, r, flag ;flag = 0;r = k = a[ i ];j = 2 * i;while ( (j <= n) && (flag == 0) ) {

if ( (j < n) && (a[ j ] < a[ j + 1] ) )j++ ;

if ( k >=a[ j ] ) flag = 1 ; else { a[ j / 2 ] = a[j] ; j = 2 * j ; }

} a[ j / 2 ] = r ; }

Page 10: Sorting s

Sorting Techniques

/* Heap Sort. Different Method*/

#include <stdio.h>#include <conio.h>

void makeheap ( int [ ], int ) ;void heapsort ( int [ ], int ) ;

void main( ){

int arr[100], n, i ;

clrscr( ) ; printf("How many "); scanf("%d",&n);

printf("Enter Numbers \n"); for( i = 0; i < n; i++ ) scanf("%d",&a[i]);

makeheap ( a, n ) ;

heapsort ( a, n ) ;

printf ( "Sorted Elements \n" ) ;for ( i = 0 ; i <= n-1 ; i++ )

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

getch( );}

void makeheap ( int x[ ], int n ){

int i, val, s, f ;for ( i = 1 ; i < n ; i++ ){

val = x[i] ;s = i ;f = ( s - 1 ) / 2 ;while ( s > 0 && x[f] < val ){

x[s] = x[f] ;s = f ;f = ( s - 1 ) / 2 ;

}x[s] = val ;

}

Page 11: Sorting s

Sorting Techniques}

void heapsort ( int x[ ], int n ){

int i, s, f, ivalue ;for ( i = n - 1 ; i > 0 ; i-- ){

ivalue = x[i] ;x[i] = x[0] ;f = 0 ;

if ( i == 1 )s = -1 ;

elses = 1 ;

if ( i > 2 && x[2] > x[1] )s = 2 ;

while ( s >= 0 && ivalue < x[s] ){

x[f] = x[s] ;f = s ;s = 2 * f + 1 ;

if ( s + 1 <= i - 1 && x[s] < x[s + 1] )s++ ;

if ( s > i - 1 )s = -1 ;

}x[f] = ivalue ;

}}

Page 12: Sorting s

Sorting Techniques/* CH9PR10.C: Merge Sort. */

#include <stdio.h>#include <conio.h>

void main( ){

int a[5] = { 11, 2, 9, 13, 57 } ;int b[5] = { 25, 17, 1, 90, 3 } ;int c[10] ;int i, j, k, temp ;

clrscr( ) ;

printf ( "Merge sort.\n" ) ;

printf ( "\nFirst array:\n" ) ;for ( i = 0 ; i <= 4 ; i++ )

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

printf ( "\n\nSecond array:\n" ) ;for ( i = 0 ; i <= 4 ; i++ )

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

for ( i = 0 ; i <= 3 ; i++ ){

for ( j = i + 1 ; j <= 4 ; j++ ){

if ( a[i] > a[j] ){

temp = a[i] ;a[i] = a[j] ;a[j] = temp ;

}if ( b[i] > b[j] ){

temp = b[i] ;b[i] = b[j] ;b[j] = temp ;

}}

}

for ( i = j = k = 0 ; i <= 9 ; ){

if ( a[j] <= b[k] )c[i++] = a[j++] ;

elsec[i++] = b[k++] ;

Page 13: Sorting s

Sorting Techniques

if ( j == 5 || k == 5 )break ;

}

for ( ; j <= 4 ; )c[i++] = a[j++] ;

for ( ; k <= 4 ; )c[i++] = b[k++] ;

printf ( "\n\nArray after sorting:\n") ;for ( i = 0 ; i <= 9 ; i++ )

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

getch( ) ;}

Page 14: Sorting s

Sorting Techniques

/** MergeSort * Internal Method that merges two sorted halves of subarray * a is an array of numbers * b is an array to place the merged result * leftPos is the left-most index of the subarray * leftEnd is the End of the first half subarray * rightPos is the index of the start of the second half * rightEnd is the right-most index of the subarray * tmpPos is the index of the temparary subarray 'b' * numElements is the number of elements * to be merged into single array */#include<stdio.h>

void Merge(int a[], int b[], int leftPos, int rightPos, int rightEnd) { int leftEnd,numElements,tmpPos,i;

leftEnd = rightPos - 1; tmpPos = leftPos; numElements = rightEnd - leftPos + 1;

// Main Loop while( leftPos <= leftEnd && rightPos <= rightEnd )

if( a[ leftPos ] <= a[ rightPos ] ) b[ tmpPos++ ] = a[ leftPos++ ]; else b[ tmpPos++ ] = a[ rightPos++ ];

while( leftPos <= leftEnd ) // Copy rest of first halfb[ tmpPos++ ] = a[ leftPos++ ];

while( rightPos <= rightEnd ) // Copy rest of second halfb[ tmpPos++ ] = a[ rightPos++ ];

// Copy b subarray back for( i = 0; i < numElements; i++, rightEnd--)

a[ rightEnd ] = b[ rightEnd ]; }

Page 15: Sorting s

Sorting Techniques

/** * Internal Method that makes recursive calls * a is an array of numbers * b is the temparary array to place merged results * left is the left-most index of the first half subarray * right is the right-most index of the second half subarray */void MergeSort( int a[], int left, int right ) { int b[100],center;

if( left < right ) {

center = ( left + right ) / 2; MergeSort( a , left, center ); MergeSort( a, center + 1, right ); Merge( a, b, left, center + 1, right );

} }

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

printf("How Many elements : "); scanf("%d",&n);

printf("Enter the Elements \n"); for( i = 0; i < n; i++ )

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

MergeSort( a, 0, n - 1 );

printf("Sorted Elements are\n"); for( i = 0; i < n; i++ )

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

}

Page 16: Sorting s

Sorting Techniques

Radix Sort

#include <stdio.h>#define MAX 5#define SHOWPASSvoid print(int *a,int n){        int i;                        for(i=0;i<n;i++)              printf("%d\t",a[i]);}void radixsort(int *a,int n){                int i,b[MAX],m=0,exp=1;                for(i=0;i<n;i++)                {                        if(a[i]>m)                                m=a[i];                }                                while(m/exp>0)                {                        int bucket[10]={0};                        for(i=0;i<n;i++)                                bucket[a[i]/exp%10]++;                        for(i=1;i<10;i++)                                bucket[i]+=bucket[i-1];                        for(i=n-1;i>=0;i--)                                b[--bucket[a[i]/exp%10]]=a[i];                        for(i=0;i<n;i++)                                a[i]=b[i];                        exp*=10; 

#ifdef SHOWPASS            printf("\nPASS   : ");

print(a,n);#endif

      }                        } 

void main()

Page 17: Sorting s

Sorting Techniques{           int arr[MAX];           int i,n;                      printf("Enter total elements (n < %d) : ",MAX);           scanf("%d",&n);                                printf("Enter %d Elements : ",n);           for(i=0;i<n;i++)                                scanf("%d",&arr[i]);                                 printf("\nARRAY  : ");           print(&arr[0],n);                      radixsort(&arr[0],n);                      printf("\nSORTED : ");           print(&arr[0],n);           printf("\n"); }

Exchange SortingThe second class of sorting algorithm that we consider comprises algorithms that sort by exchanging   pairs of items until the sequence is sorted. In general, an algorithm may exchange adjacent elements as well as widely separated ones.

In fact, since the insertion sorts considered in the preceding section accomplish the insertion by swapping adjacent elements, insertion sorting can be considered as a kind of exchange sort. The reason for creating a separate category for insertion sorts is that the essence of those algorithms is insertion into a sorted list. On the other hand, an exchange sort does not necessarily make use of such a sorted list.

Bubble Sort Quicksort Running Time Analysis Average Running Time Selecting the Pivot