Advertisement
RajibHossen

Quick sort count comparison

May 23rd, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cstring>
  4. #include <cctype>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <list>
  8. #include <map>
  9. #include <queue>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #include <stdio.h>
  14. #include <sstream>
  15. #include <utility>
  16. #include <vector>
  17. #define INT_MAX 2147483647
  18. #define INT_MIN -2147483648
  19. #define pi acos(-1.0)
  20. #define N 1000000
  21. #define LL long long
  22.  
  23. using namespace std;
  24.  
  25. int comparisonCount = 0;
  26.  
  27. int chooseMedianOfThreeAsPivot(int a[],int start, int end) {
  28.  
  29.         int pivotIndex = -1;
  30.  
  31.         int mid = ((end - start) / 2) + start;
  32.         int b[3];
  33.         b[0] = a[start];
  34.         b[1] = a[mid];
  35.         b[2] = a[end];
  36.  
  37.         sort(b,b+3);
  38.         if (b[1] == a[start]) {
  39.             pivotIndex = start;
  40.         }else if(b[1] == a[mid]) {
  41.             pivotIndex = mid;
  42.         }else {
  43.             pivotIndex = end;
  44.         }
  45.  
  46.         // Move pivot to start index.
  47.         int temp = a[start];
  48.         a[start] = a[pivotIndex];
  49.         a[pivotIndex] = temp;
  50.  
  51.         return start;
  52. }
  53.  
  54. int partition(int a[],int l,int r,int pivotIndex)
  55. {
  56.     int arrayLength = r - l;
  57.     comparisonCount += arrayLength;
  58.     int p,i,j;
  59.     p = a[pivotIndex];
  60.     i = l+1;
  61.     for(j=l+1;j<=r;j++)
  62.     {
  63.         if(a[j]<p)
  64.         {
  65.              int t = a[i];
  66.              a[i] = a[j];
  67.              a[j] = t;
  68.              i++;
  69.         }
  70.     }
  71.     int t=a[l];
  72.     a[l] = a[i-1];
  73.     a[i-1] = t;
  74.     return i-1;
  75. }
  76.  
  77. void quick_sort(int *a,int l,int r)
  78. {
  79.     if (r <= l) {
  80.             return;
  81.         }
  82.     if(l<r)
  83.     {
  84.         int pivotIndex = chooseMedianOfThreeAsPivot(a,l,r);
  85.         int s = partition(a,l,r,pivotIndex);
  86.         quick_sort(a,l,s-1);
  87.         quick_sort(a,s+1,r);
  88.  
  89.     }
  90. }
  91.  
  92. int main()
  93. {
  94.     #ifndef ONLINE_JUDGE
  95.        freopen("data.txt", "r", stdin);
  96.        freopen("output.txt","w",stdout);
  97.     #endif
  98.  
  99.     int array[100005];
  100.     for(int i=0;i<10000;i++)
  101.         cin>>array[i];
  102.  
  103.    // partition(array,1,10000);
  104.     quick_sort(array,0,9999);
  105.  
  106.     for(int i=0;i<10000;i++)
  107.         cout<<array[i]<<"\n";
  108.  
  109.     cout<<"\n\n\n";
  110.     cout<<comparisonCount<<"\n";
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement