Advertisement
sailorbob74133

DSA Maman 13 Q1

Apr 24th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int findpivot(int A[], int first, int last) {
  4.     return last;
  5. }
  6.  
  7. #define swap(A, B) do { int temp = A;  A = B; B = temp; } while (0)
  8.  
  9. // Lomuto Partition
  10. int partition(int A[], int first, int last) {
  11.     int pivotIndex = findpivot(A, first, last);
  12.  
  13.     int pivot = A[pivotIndex];
  14.     int i = first - 1;
  15.  
  16.     for (int j = first; j != last; j++)
  17.         if ( A[j] <= pivot ) {
  18.             i++;
  19.             swap( A[i], A[j] );
  20.         }
  21.  
  22.     swap( A[ i + 1 ], A[ last ] );
  23.     return i + 1;
  24. }
  25.  
  26. #define printArray(A, first, last) \
  27.     do { \
  28.         if ( first <= last ) \
  29.             for (int i = first; i != last + 1; i++) \
  30.                 printf("%d ", A[i]); \
  31.         printf("\n"); \
  32.     } while (0)
  33.  
  34.  
  35. void quicksort(int A[], int first, int last) {
  36.     printf("p = %d\tr = %d\n", first+1, last+1);
  37.  
  38.     if ( first < last ) {
  39.         printArray(A, first, last);
  40.         printf("Call Partition. ");
  41.         int pivot = partition(A, first, last);
  42.         printf("Done Partitioning: the pivot is %d\n", A[pivot]);
  43.         printArray(A, first, last);
  44.  
  45.         printf("Recurse on ");
  46.         quicksort(A, first, pivot - 1);
  47.  
  48.         printf("Recurse on ");
  49.         quicksort(A, pivot+1, last);
  50.         printf("The range p = %d\tr = %d is sorted\n", first+1, last+1);
  51.         printArray(A, first, last);
  52.     } else printf("Nothing to do\n");
  53. }
  54.  
  55. // i is alway relative to first
  56. int select(int A[], int first, int last, int i) {
  57.     printf("p = %d\tr = %d\ti = %d\n", first+1, last+1, i);
  58.     printArray(A, first, last);
  59.  
  60.     if ( first == last )
  61.         return A[first];
  62.  
  63.     printf("Call Partition\n");
  64.     int pivotIndex = partition(A, first, last);
  65.     printf("Done Partitioning: the pivot is %d\n", A[pivotIndex]);
  66.     printArray(A, first, last);
  67.  
  68.     int relativePivotIndex = pivotIndex - first + 1;
  69.     printf("k == %d\n", relativePivotIndex);
  70.  
  71.     if ( relativePivotIndex == i )
  72.         return A[pivotIndex];  // The pivot is the answer
  73.     else if ( i < relativePivotIndex ) {
  74.         printf("Recurse on ");
  75.         return select(A, first, pivotIndex - 1, i );
  76.     } else {
  77.         printf("Recurse on ");
  78.         return select(A, pivotIndex + 1, last, i - relativePivotIndex );
  79.     }
  80. }
  81.  
  82. int main(void) {
  83.     int A[] = { 55, 3, 8, 4, 7, 1, 10, 9, 2, 10, 10, 10, 6, 11 };
  84.  
  85. #if 0
  86.     quicksort(A, 0, sizeof(A)/sizeof(A[0]) - 1);
  87. #else
  88.     // the 10th place is locate in position 9 since the array's are 0 based
  89.     int i = select(A, 0, sizeof(A)/sizeof(A[0]) - 1, 10);
  90.     printf("The 10th order statistic is %d\n", i);
  91. #endif
  92.  
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement