Advertisement
paulogp

Quicksort

Sep 3rd, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. // xcode
  2. // paulogp
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. #define MAX_ELEMENTS 30
  9.  
  10. void swap_elements (int *x, int *y);
  11. int get_pivot (int the_min, int the_max);
  12. void quicksort (int the_list[], int the_min, int the_max);
  13. void print_list (int the_list[]);
  14.  
  15. void swap_elements (int *the_a, int *the_b)
  16. {
  17.     int the_temp;
  18.    
  19.     the_temp = *the_a;
  20.     *the_a = *the_b;
  21.     *the_b = the_temp;
  22. }
  23.  
  24. int get_pivot (int the_min, int the_max)
  25. {
  26.     // middle
  27.     return ((the_min + the_max) / 2);
  28. }
  29.  
  30. void quicksort (int the_list[], int the_min, int the_max)
  31. {
  32.     int the_key, i, j, the_pivot;
  33.    
  34.     if (the_min < the_max)
  35.     {
  36.         the_pivot = get_pivot (the_min, the_max);
  37.        
  38.         swap_elements (&the_list[the_min], &the_list[the_pivot]);
  39.         the_key = the_list[the_min];
  40.        
  41.         i = the_min + 1;
  42.         j = the_max;
  43.        
  44.         while (i <= j)
  45.         {
  46.             while ((i <= the_max) && (the_list[i] <= the_key))
  47.                 i++;
  48.            
  49.             while ((j >= the_min) && (the_list[j] > the_key))
  50.                 j--;
  51.            
  52.             if (i < j)
  53.                 swap_elements (&the_list[i], &the_list[j]);
  54.         }
  55.        
  56.         // swap two elements
  57.         swap_elements (&the_list[the_min], &the_list[j]);
  58.        
  59.         // recursively sort the lesser list
  60.         quicksort (the_list, the_min, (j - 1));
  61.         quicksort (the_list, (j + 1), the_max);
  62.     }
  63. }
  64.  
  65. void print_list (int the_list[])
  66. {
  67.     int i = 0;
  68.    
  69.     while (the_list[i] != '\0')
  70.     {
  71.         printf("%d\t", the_list[i]);
  72.         i += 1;
  73.     }
  74. }
  75.  
  76. int main (int argc, const char * argv[])
  77. {
  78.     int the_list[MAX_ELEMENTS];
  79.    
  80.     int i = 0;
  81.    
  82.     // seed the random sequence generated by rand()
  83.     srand ((unsigned)time(NULL));
  84.    
  85.     // generate random numbers and fill them to the list
  86.     for (i = 0; i < MAX_ELEMENTS; i++)
  87.     {
  88.         the_list[i] = (rand() % 20) + 1; // random from 1 to 20
  89.     }
  90.    
  91.     printf("the list before sorting is:\n");
  92.     print_list (the_list);
  93.    
  94.     // sort the list using quicksort
  95.     quicksort (the_list, 0, MAX_ELEMENTS-1);
  96.    
  97.     // print the result
  98.     printf ("\nthe list after sorting using quicksort algorithm:\n");
  99.     print_list (the_list);
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement