Posts

Showing posts with the label Sorting

Selection Sort

Image
Selection Sort A selection sort is a sorting techniqlue in which successive elements are selected in order and paced into their proper sorted positions. By starting from the first element and using a nest of for loops, a pass through the array is made to locate the minimum value. Once this is found, it 1s placed in the first position of the array (position 0). Then the remaining elements is made to find the next smallest element, which is placed in the second position (position 1), and so on. Once the next to last element is compared with the last element, all the elements have been sorted into ascending order. The algorithm works as follows: Find the minimum value in the list. Swap it with the value in the first position. Repeat the above steps for the remainder of the list (starting at the second position and advancing each time). Algorithm for Selection Sort This algorithm sorts the array list with n elements Initialization, Set loc=o

Quick Sort

Image
Quick Sort It is also called partition exchange sort because it works by partitioning the array to be sorted. Basic Idea: Let x be an array and n be the number of elements in the array. Let us choose an element (called pivot) a from a specific position within the array (for example, a can be chosen as the first element so that a=x[o]). Now suppose that the elements of array x are partitioned in such a way that a is placed into position j of the array and the following conditions hold: Each of the elements in positions j through j-1 is less than or equal to a. Each of the elements in positions j+1 through n-1 is greater than or equal to a. If these two conditions hold for a particular a and j, a is the jth smallest element of array x, so that remains in position j when the array is completely sorted. lf the same process is repeated for sub-arrays x[o] through x[j-1] and x[j+1] through x[n-1] and any sub arrays created by the process in successive iterations,

C Program For Selection Sort | C Programming

C Program For Selection Sort by Generating Random Numbers and Using time.h Function #include <stdio.h> #include <time.h> #include <stdlib.h> void selection_sort ( int [], int ); int main () {     int list [ 100 ], n , i ;     time_t t ;     printf ( "Enter the max number \n " );     scanf ( " %d " , & n );     srand (( unsigned ) time (& t ));     for ( i = 0 ; i < n ; i ++)     {         list [ i ] = rand () % 100 ;     }     for ( i = 0 ; i < n ; i ++)     {         printf ( " %d \t " , list [ i ]);     }     selection_sort ( list , n );     printf ( " \n Time taken to complete the selectionsort %u " , clock () / CLOCKS_PER_SEC);     printf ( " \n The sorted list is: \n " );     for ( i = 0 ; i < n ; i ++)         printf ( " %d \t " , list [ i ]);     return 0 ; } void selection_sort ( int list [], int n ) {     int temp , loc , j ;     for ( loc = 0 ; loc < n -

C Program For Merge Sort | C Programming

C Program For Merge Sort by Generating Random Numbers and Using time.h Function #include <stdio.h> #include <stdlib.h> #include <time.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 ;     time_t t ;     printf ( "Enter the max number \n " );     scanf ( " %d " , & n );     srand (( unsigned ) time (& t ));     for ( i = 0 ; i < n ; i ++)     {         a [ i ] = rand () % 100 ;     }     for ( i = 0 ; i < n ; i ++)     {         printf ( " %d \t " , a [ i ]);     }     mergesort ( a , 0 , n - 1 );     printf ( " \n\n Time taken to complete the mergesort %u \n " , clock () / CLOCKS_PER_SEC);     printf ( " \n Sorted array is : \n " );     for ( i = 0 ; i < n ; i ++)         printf ( " %d \t " , a [ i ]);     return 0 ; } void mergesort ( int a [], int i , int j ) {

C Program For Quick Sort | C Programming

C Program For Quick Sort To Generate Random Number and Sort it #include <stdio.h> #include <stdlib.h> void quicksort ( int number [ 25 ], int first , int last ) {     int i , j , pivot , temp ;     if ( first < last )     {         pivot = first ;         i = first ;         j = last ;         while ( i < j )         {             while ( number [ i ] <= number [ pivot ] && i < last )                 i ++;             while ( number [ j ] > number [ pivot ])                 j --;             if ( i < j )             {                 temp = number [ i ];                 number [ i ] = number [ j ];                 number [ j ] = temp ;             }         }         temp = number [ pivot ];         number [ pivot ] = number [ j ];         number [ j ] = temp ;         quicksort ( number , first , j - 1 );         quicksort ( number , j + 1 , last );     } } int main () {     int i , count , number [ 1000 ];     printf (

C Program for Insertion Sort | C Programming

C Program For Insertion Sort Using time.h Function and Generating Random Number #include <math.h> #include <time.h> #include <stdlib.h> #include <stdio.h> void insertionSort ( int arr [], int n ) {     int i , key , j ;     for ( i = 1 ; i < n ; i ++)     {         key = arr [ i ];         j = i - 1 ;         while ( j >= 0 && arr [ j ] > key )         {             arr [ j + 1 ] = arr [ j ];             j = j - 1 ;         }         arr [ j + 1 ] = key ;     } } void printArray ( int arr [], int n ) {     int i ;     for ( i = 0 ; i < n ; i ++)         printf ( " %d  " , arr [ i ]);     printf ( " \n " ); } int main () {     int arr [ 100 ], n , i ;     time_t t ;     printf ( "Enter the max number \n " );     scanf ( " %d " , & n );     srand (( unsigned ) time (& t ));     for ( i = 0 ; i < n ; i ++)     {         arr [ i ] = rand () % 100 ;     }