Advertisement
DevilDaga

Sorting Execution Times

Jan 26th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <malloc.h>
  6.  
  7. #define     NUMELEMENTS 1000000
  8. #define     NUMDIGITS   5
  9. const int   VERIFY = 0;
  10.  
  11. inline void swap ( int *left, int *right )
  12. {
  13.     int temp = *left;
  14.     *left = *right;
  15.     *right = temp;
  16. }
  17.  
  18. // Bubble Sort method which returns time elapsed while sorting
  19. double bubblesort ( int a[ ], int n )
  20. {
  21.     clock_t time1 = clock ( );
  22.     int i, j;
  23.     for ( i = 0; i < n - 1; i++ )
  24.         for ( j = 0; j<n - 1; j++ )
  25.             if ( a[ j ]>a[ j + 1 ] )
  26.                 swap ( &a[ j ], &a[ j + 1 ] );
  27.     return (double) ( clock ( ) - time1 ) / CLOCKS_PER_SEC;
  28. }
  29.  
  30. // Selection Sort method which returns time elapsed while sorting
  31. double selectionsort ( int a[ ], int n )
  32. {
  33.     clock_t time1 = clock ( );
  34.     int i, j, min, pos;
  35.     for ( i = 0; i < n - 1; i++ )
  36.     {
  37.         min = a[ pos = i ];
  38.         for ( j = i + 1; j < n; j++ )
  39.             if ( a[ j ] < min )
  40.                 min = a[ pos = j ];
  41.         a[ pos ] = a[ i ];
  42.         a[ i ] = min;
  43.     }
  44.     return (double) ( clock ( ) - time1 ) / CLOCKS_PER_SEC;
  45. }
  46.  
  47. // Insertion Sort method which returns time elapsed while sorting
  48. double insertionsort ( int a[ ], int n )
  49. {
  50.     clock_t time1 = clock ( );
  51.     int i, j, val;
  52.     for ( i = 0; i<n - 1; i++ )
  53.     {
  54.         val = a[ i + 1 ];
  55.         for ( j = i; j >= 0; j-- )
  56.         {
  57.             if ( a[ j ]>val )
  58.                 a[ j + 1 ] = a[ j ];
  59.             else
  60.                 break;
  61.         }
  62.         a[ j + 1 ] = val;
  63.     }
  64.     return (double) ( clock ( ) - time1 ) / CLOCKS_PER_SEC;
  65. }
  66.  
  67. void merge ( int x[ ], int lb, int m, int ub )
  68. {
  69.     int i = lb, j = m + 1, k = lb;
  70.     while ( i <= m && j <= ub )
  71.         if ( x[ i ] < x[ j ] )
  72.             x[ k++ ] = x[ i++ ];
  73.         else
  74.             x[ k++ ] = x[ j++ ];
  75.     while ( i <= m )
  76.         x[ k++ ] = x[ i++ ];
  77.     while ( j <= ub )
  78.         x[ k++ ] = x[ j++ ];
  79. }
  80.  
  81. void mergesort_worker ( int a[ ], int l, int u )
  82. {
  83.     if ( l < u )
  84.     {
  85.         int mid = ( l + u ) / 2;
  86.         mergesort_worker ( a, l, mid );
  87.         mergesort_worker ( a, mid + 1, u );
  88.         merge ( a, l, mid, u );
  89.     }
  90. }
  91.  
  92. void quicksort_worker ( int a[ ], int low, int high )
  93. {
  94.     int pivot, j, i;
  95.     if ( low < high )
  96.     {
  97.         pivot = i = low;
  98.         j = high;
  99.         while ( i < j )
  100.         {
  101.             while ( ( a[ i ] <= a[ pivot ] ) && ( i < high ) )
  102.                 i++;
  103.             while ( a[ j ] > a[ pivot ] )
  104.                 j--;
  105.             if ( i < j )
  106.                 swap ( &a[ i ], &a[ j ] );
  107.         }
  108.         swap ( &a[ pivot ], &a[ j ] );
  109.         quicksort_worker ( a, low, j - 1 );
  110.         quicksort_worker ( a, j + 1, high );
  111.     }
  112. }
  113.  
  114. void print_times ( double times[ ], int n )
  115. {
  116.     int i, mul = 1;
  117.     for ( i = 0; i < n; i++ )
  118.     {
  119.         mul *= 10;
  120.         printf ( "%d\t:\t%.3fs\n",
  121.                  mul,
  122.                  times[ i ] );
  123.     }
  124. }
  125.  
  126. // Merge Sort method which returns time elapsed while sorting
  127. double mergesort ( int a[ ], int n )
  128. {
  129.     clock_t time1 = clock ( );
  130.     mergesort_worker ( a, 0, n );
  131.     return (double) ( clock ( ) - time1 ) / CLOCKS_PER_SEC;
  132. }
  133.  
  134. // Quick Sort method which returns time elapsed while sorting
  135. double quicksort ( int a[ ], int n )
  136. {
  137.     clock_t time1 = clock ( );
  138.     quicksort_worker ( a, 0, n );
  139.     return (double) ( clock ( ) - time1 ) / CLOCKS_PER_SEC;
  140. }
  141.  
  142. // Check if array is sorted
  143. int verify ( int a[ ], int n )
  144. {
  145.     int i;
  146.     for ( i = 0; i != n - 1; i++ )
  147.         if ( a[ i ] > a[ i + 1 ] )
  148.             return 0;
  149.     return 1;
  150. }
  151.  
  152. int main ( )
  153. {
  154.     int *rand_collection, *a,
  155.         check[ 5 ] = { 0 },                 // To verify that the array is sorted
  156.         i, n = 1;
  157.     double
  158.         bubble_times[ NUMDIGITS ],
  159.         selection_times[ NUMDIGITS ],
  160.         insertion_times[ NUMDIGITS ],
  161.         merge_times[ NUMDIGITS ],
  162.         quick_times[ NUMDIGITS ];
  163.     rand_collection = (int*) malloc ( NUMELEMENTS * sizeof ( int ) );
  164.     a = (int *) malloc ( NUMELEMENTS * sizeof ( int ) );
  165.     srand ( (unsigned) clock ( ) );
  166.     for ( i = 0; i < NUMELEMENTS; i++ )
  167.         rand_collection[ i ] = rand ( ) % 1000;
  168.     for ( i = 0; i < NUMDIGITS; i++ )
  169.     {
  170.         n = n * 10;
  171.         memcpy ( a, rand_collection, n * sizeof ( int ) );
  172.         bubble_times[ i ] = bubblesort ( a, n );
  173.         if ( VERIFY )
  174.             check[ 0 ] |= verify ( a, n );
  175.         memcpy ( a, rand_collection, n * sizeof ( int ) );
  176.         selection_times[ i ] = selectionsort ( a, n );
  177.         if ( VERIFY )
  178.             check[ 1 ] |= verify ( a, n );
  179.         memcpy ( a, rand_collection, n * sizeof ( int ) );
  180.         insertion_times[ i ] = insertionsort ( a, n );
  181.         if ( VERIFY )
  182.             check[ 2 ] |= verify ( a, n );
  183.         memcpy ( a, rand_collection, n * sizeof ( int ) );
  184.         merge_times[ i ] = mergesort ( a, n );
  185.         if ( VERIFY )
  186.             check[ 3 ] |= verify ( a, n );
  187.         memcpy ( a, rand_collection, n * sizeof ( int ) );
  188.         quick_times[ i ] = quicksort ( a, n );
  189.         if ( VERIFY )
  190.             check[ 4 ] |= verify ( a, n );
  191.     }
  192.     printf ( "Execution times for Bubble Sort:\n" );
  193.     print_times ( bubble_times, NUMDIGITS );
  194.     printf ( "Execution times for Selection Sort:\n" );
  195.     print_times ( selection_times, NUMDIGITS );
  196.     printf ( "Execution times for Insertion Sort:\n" );
  197.     print_times ( insertion_times, NUMDIGITS );
  198.     printf ( "Execution times for Merge Sort:\n" );
  199.     print_times ( merge_times, NUMDIGITS );
  200.     printf ( "Execution times for Quick Sort:\n" );
  201.     print_times ( quick_times, NUMDIGITS );
  202.     if ( VERIFY )
  203.         for ( i = 0; i < 5; i++ )
  204.             printf ( "%d\t", check[ i ] );
  205.     getchar ( );
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement