1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include<stdio.h> void mergesort ( int [] , int , int ); void merge ( int [] , int , int , int ); main() { int a[50] , n , i , j , first , last , mid ; printf ("enter the number of elements\n"); scanf ("%d",&n); printf ("enter all elements\n"); for ( i = 0 ; i < n ; i++) scanf ("%d",&a[ i ] ); mergesort ( a , 0 , n-1 ); printf ("the sorted array is ..\n"); for ( i = 0 ; i < n ; i++ ) printf ("%d\n", a[ i ] ); } void mergesort ( int a[50] , int first , int last ) { int mid , i ; if ( first < last ) { mid = ( first + last ) / 2 ; mergesort ( a , first , mid ) ; mergesort ( a , mid+1 , last ); merge ( a , first , mid , last ); } } void merge ( int a[50] , int first , int mid , int last ) { int temp[50] , k , i , j ; k = first ; i = first ; j = mid+1 ; while ( i < = mid && j < = last ) { if ( a [ i ] < a [ j ] ) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; } } while ( i < = mid ) { temp[k] = a[i]; k++; i++; } while ( j < = last ) { temp[k] = a[j]; k++; j++; } for ( i = first ; i < = last ; i++ ) { a[ i ] = temp[ i ] ; } } |
New Android apps UniqueKey
Thursday, 28 August 2014
MERGE SORT
QUICK SORT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include<stdio.h> #include<conio.h> void quick ( int [ ] , int , int ) ; main() { int a[50] , first , last , i , j , n ; printf("enter the no. of element in array\n"); scanf("%d",&n); printf("enter all elements\n"); for ( i = 0 ; i < n ; i++ ) scanf("%d",&a[i]); quick( a , 0 , n-1 ); printf("sorted array\n"); for ( i = 0 ; i < n ; i++ ) printf("%d\t",a[i]); getch(); } void quick ( int a[ ] , 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 ; quick ( a , first , j-1 ) ; quick( a , j+1 , last ) ; } } _________________________________________________________________________________ |
INSERTION SORT.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /* a c program to perform insertion sort */ #include<stdio.h> #include<stdlib.h> main() { int a[50] , i , j , index , n; printf("enter the number of element in array\n"); scanf("%d",&n); printf("enter all the elements of array\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=1;i<n;i++) { index = a[i]; for(j=i ; j>0 && a[j-1]>index ; j--) { a[j] = a[j-1]; } a[j] = index; } printf("sorted array in Ascending Order is \n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } } |
SELECTION SORT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /* A C program to perform insertion sort */ #include<stdio.h> #include<stdlib.h> main() { int a[50] , i , j , n ,temp , min; printf("enter the number of elements in array\n"); scanf("%d",&n); // accepts the number of elements. printf("enter all the elements \n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); // accepts all the elements of the array. } for(i=0;i<n-1;i++) { min=i; for(j=i+1;j<n;j++) { if(a[j]<a[min]) min=j; } temp = a[i]; // swapping. a[i] = a[min]; a[min] = temp; } printf("sorted array in Ascending Order is \n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } } |
BUBBLE SORT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /* a c proram to perform bubble sorting */ #include<stdio.h> #include<stdlib.h> main() { int a[50] , n ,i , j; int temp; // use temp variable for swapping. printf("enter the number of elements in array\n"); scanf("%d",&n); // accepts number of elements. printf("enter all the elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); // accepts all the elements of the array. } for(i=0;i<n-1;++i) { for(j=0;j<n-i-1;++j) { if(a[j]>a[j+1]) { temp = a[j]; // swapping. a[j] = a[j+1]; a[j+1] = temp; } } } printf("Array in Ascending order ::\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); } // end of main function. |
Subscribe to:
Posts (Atom)