Advertisement
HimikoWerckmeister

Partition

Jul 11th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. template <class T>
  2. // This function finds the median of the three values from the first, middle, and last elements
  3. // The return type is the template type with an array, a left index, and a right index as parameters
  4. // required for the requested calculation. In our case, this calculation is discerning where to place the
  5. // the pivot in the array so we can begin partitioning it.
  6. T Median3Pivot(T arr[], int left, int right)
  7. {
  8.     //int iMid = (right+left)/2;
  9.     int iMid = left+(right-left)/2;// prevent overflow errors when calculating the middle value
  10.     if(arr[left] > arr[iMid])
  11.     {
  12.         swap(&arr[left], &arr[iMid]);
  13.     }
  14.     if(arr[iMid]> arr[right])
  15.     {
  16.         swap(&arr[iMid], &arr[right]);
  17.     }
  18.     if(arr[left]> arr[iMid])
  19.     {
  20.         swap(&arr[left],&arr[iMid]);
  21.     }
  22.  
  23.     swap(&arr[iMid], &arr[right-1]);
  24.    
  25.     return arr[right-1];
  26. }
  27.  
  28. // Partition helper function so we begin partitioning for our sub-arrays
  29. // Precondition after finding the median of the three entries,
  30. //  the first, middle, and last entries are sorted with respect to each other thus
  31. //  pivot is at array index right-1
  32. template <class T>
  33. int Partition(T arr[], int left, int right, int& iCompCnts)
  34. {
  35.     Median3Pivot(arr, left, right);
  36.     // the two lines below are the ones that select the pivot
  37.     int iPivotIdx = right-1;
  38.     T iPivot = arr[iPivotIdx];
  39.  
  40.     int i = left+1, j = right-2; // i represents the index from left while j represents the index from right
  41.     bool bDone = false;
  42.     // Bool flag to indicate when we are done the swapping of the elements when we compare the elements arr[i] and arr[j]
  43.     // wrt to pivot
  44.     while(!bDone)
  45.     {
  46.         // Move the pivot point to the right:
  47.         while(arr[i]<iPivot)
  48.         {
  49.             i++;
  50.             iCompCnts++;
  51.  
  52. #ifdef _DEBUG_CMPT225_ASSIGNMENT3
  53.             s_iCounter++;
  54. #endif
  55.         }
  56.  
  57.         iCompCnts++;
  58. #ifdef _DEBUG_CMPT225_ASSIGNMENT3
  59.             s_iCounter++;
  60. #endif
  61.  
  62.         // Move the pivot point to the left:
  63.         while(iPivot<arr[j])
  64.         {
  65.             j--;
  66.             iCompCnts++;
  67.  
  68. #ifdef _DEBUG_CMPT225_ASSIGNMENT3
  69.                 s_iCounter++;
  70. #endif
  71.         }
  72.         iCompCnts++;
  73.  
  74. #ifdef _DEBUG_CMPT225_ASSIGNMENT3
  75.                 s_iCounter++;
  76. #endif
  77.         // if the right temp. pivot is indeed on the right of the
  78.         // left temp. pivot, then swap:
  79.         if (i<j)
  80.         {
  81.             swap(&arr[i], &arr[j]);
  82.             i++;
  83.             j--;
  84.         }  
  85.         else    // the loop is done:
  86.             bDone = true;
  87.     }
  88.  
  89.     // Place pivot point at the right position and marked it too:  
  90.     // Don't want to swap elements that are adjacent to each other so we have a three parameter insertion sort guarding
  91.     // against this degenerate case
  92.     swap(&arr[iPivotIdx],&arr[i]);
  93.     iPivotIdx = i;
  94.  
  95.     return iPivotIdx;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement