Guest User

Vikram Menon

a guest
Sep 17th, 2009
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. /*
  2. Sort an array of integers in descending order by different methods.
  3. */
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. const int MAX = 20;
  10.  
  11. class heap                      // to facilitate heap sorting
  12. {
  13.     int *ar;
  14.     int l;
  15.  
  16. public:
  17.     void heapify(int x = 0)
  18.     {
  19.         if(x >= l/2)
  20.             return;
  21.         heapify(2*x+1);
  22.         heapify(2*x+2);
  23.         if(ar[2*x+1] > ar[2*x+2])       // right subnode is larger
  24.         {
  25.             if(ar[x] < ar[2*x+1])
  26.             {
  27.                 int temp = ar[x];
  28.                 ar[x] = ar[2*x+1];
  29.                 ar[2*x+1] = temp;
  30.             }
  31.         }
  32.         else                            // left subnode is larger
  33.         {
  34.             if(ar[x] < ar[2*x+2])
  35.             {
  36.                 int temp = ar[x];
  37.                 ar[x] = ar[2*x+2];
  38.                 ar[2*x+2] = temp;
  39.             }
  40.         }
  41.     }
  42.  
  43.     void push(int v)
  44.     {
  45.         int i = l-1;
  46.         while(ar[i] > -MAX && i > l/2)
  47.             i--;
  48.         ar[i] = v;                      // empty node at lowest tier
  49.         heapify();
  50.     }
  51.  
  52.     heap(int *a, int n)
  53.     {
  54.         for(l=1; l <= n; l*=2);         // nodes in tree
  55.         l--;                        // l = {1,3,7,15...}
  56.         ar = new int[l];
  57.         for(int i=0; i < l; i++)
  58.             ar[i] = -MAX;
  59.         for(int i=0; i < n; i++)
  60.             push(a[i]);
  61.     }
  62.  
  63.     ~heap()
  64.     {
  65.         delete[] ar;
  66.     }
  67.  
  68.     int pop()
  69.     {
  70.         int temp = ar[0];
  71.         ar[0] = -MAX;
  72.         heapify();
  73.         return temp;
  74.     }
  75. };
  76.  
  77. void swap(int *a, int p1, int p2)       // swap two integers within an array
  78. {
  79.     int temp = a[p1];
  80.     a[p1] = a[p2];
  81.     a[p2] = temp;
  82. }
  83.  
  84. void simplesort(int *a, int n)          // position-by-position comparison
  85. {
  86.     int temp;
  87.     for(int i=0; i < n; i++)
  88.         for(int j=i; j < n; j++)
  89.             if(a[j] > a[i])
  90.             {
  91.                 temp = a[i];
  92.                 a[i] = a[j];
  93.                 a[j] = temp;
  94.             }
  95. }
  96.  
  97. void bubblesort(int *a, int n)          // compare consecutive elements repeatedly
  98. {
  99.     int temp;
  100.     for(int i=0; i < n; i++)
  101.         for(int j=0; j < n-1-i; j++)
  102.             if(a[j+1] > a[j])
  103.             {
  104.                 temp = a[j];
  105.                 a[j] = a[j+1];
  106.                 a[j+1] = temp;
  107.             }
  108. }
  109.  
  110. void mergesort(int *a, int n)           // sort on merging sublists
  111. {
  112.     if(n == 1)
  113.         return;
  114.     int c[n];
  115.     int *b = a+(n/2);
  116.     mergesort(a, n/2);
  117.     mergesort(b, n-(n/2));
  118.     int t1=0, t2=0;
  119.     for(int i=0; i < n; i++)
  120.     {
  121.         c[i] = 0;
  122.         if(t1 >= n/2)
  123.             c[i] = b[t2++];
  124.         else if(t2 >= n-(n/2))
  125.             c[i] = a[t1++];
  126.         else if(a[t1] > b[t2])
  127.             c[i] = a[t1++];
  128.         else
  129.             c[i] = b[t2++];
  130.     }
  131.     for(int i=0; i < n; i++)
  132.         a[i] = c[i];
  133. }
  134.  
  135. void heapsort(int *a,int n)             // sort using the heap class
  136. {
  137.     heap h(a, n);                   // parameterised constructor call
  138.     for(int i=0; i < n; i++)
  139.         a[i] = h.pop();
  140. }
  141.  
  142. void quicksort(int *a, int n)       // choose a pivot P and sort as ( >= P | < P )
  143. {
  144.     if(n <= 1)
  145.         return;
  146.     int i, pivot = n/2;
  147.     swap(a, pivot, 0);
  148.     pivot = 0;
  149.     for(i=1; i < n; i++)
  150.     {
  151.         if(a[i] >= a[pivot])
  152.         {
  153.             swap(a, i, pivot);
  154.             pivot++;
  155.             if(i != pivot)
  156.             swap(a, i, pivot);
  157.         }
  158.     }
  159.     quicksort(a, pivot);
  160.     quicksort(a + pivot, n - pivot);
  161. }
  162.  
  163. void selectsort(int *a, int n)          // select ith largest, place at (i-1)th location
  164. {
  165.     int max, temp;
  166.     for(int i=0; i < n-1; i++)
  167.     {
  168.         max = i;
  169.         for(int j=i; j < n; j++)
  170.             if(a[j] > a[max])
  171.                 max = j;
  172.         temp = a[max];              // store a[max] at a[i]
  173.         a[max] = a[i];
  174.         a[i] = temp;
  175.     }
  176. }
  177.  
  178. void insertsort(int *a, int n)          // insert a[i] into sorted sublist
  179. {
  180.     int temp;
  181.     for(int i=1; i < n; i++)
  182.         for(int j=i; j > 0 && a[j] > a[j-1]; j--)   // move this element into place
  183.         {
  184.             temp = a[j];
  185.             a[j] = a[j-1];
  186.             a[j-1] = temp;
  187.         }
  188. }
  189.  
  190. void countsort(int *a, int n)           // use sparse array to hold count of values
  191. {
  192.     int b[MAX];
  193.     int top = 0;
  194.     for(int i=0; i < MAX; i++)
  195.         b[i] = 0;
  196.     for(int i=0; i < n; i++)
  197.         b[a[i]%MAX]++;
  198.     for(int i=MAX-1; i >= 0; i--)
  199.         while(b[i]--)
  200.             a[top++] = i;
  201. }
  202.  
  203. void bucketsort(int *a, int n)          // split data into buckets to sort separately
  204. {
  205.     int bkt[2][n], t1 = 0, t2 = 0;      // two buckets: +ve and -ve
  206.     for(int i=0; i < n; i++)
  207.         if(a[i] > 0)
  208.             bkt[0][t1++] = a[i];
  209.         else
  210.             bkt[1][t2++] = a[i];
  211.     selectsort(bkt[0], t1);
  212.     selectsort(bkt[1], t2);
  213.     for(int i=0; i < t1; i++)
  214.         a[i] = bkt[0][i];
  215.     for(int i=0; i < t2; i++)
  216.         a[t1+i] = bkt[1][i];
  217. }
  218.  
  219. void radixsort(int *a, int n)       // sort by radix, least to most significant
  220. {
  221.     int max = -MAX;
  222.     for(int i=0; i < n; i++)
  223.         if(a[i] > max)
  224.             max = a[i];
  225.     for(int ctr=10; max > 0; ctr*=10, max/=10)
  226.     {
  227.         int div = ctr/10;
  228.         for(int i=0; i < n; i++)
  229.             for(int j=0; j < n-1-i; j++)
  230.                 if(((a[j+1]%ctr)/div) > ((a[j]%ctr)/div))
  231.                 {
  232.                     int temp = a[j];
  233.                     a[j] = a[j+1];
  234.                     a[j+1] = temp;
  235.                 }
  236.     }
  237. }
  238.  
  239. void dualpivot(int *a, int n)       // choose pivots P1 > P2 and sort as ( >= P1 | > P2 | <= P2 )
  240. {
  241.     if(n <= 1)
  242.         return;
  243.     int i, pivot1 = n/3, pivot2 = 2*n/3;
  244.     swap(a, pivot1, 0);
  245.     swap(a, pivot2, n-1);
  246.     pivot1 = 0;
  247.     pivot2 = n-1;
  248.     if(a[pivot1] < a[pivot2])
  249.         swap(a, pivot1, pivot2);
  250.     for(i=pivot1+1; i < pivot2; i++)
  251.     {
  252.         if(a[i] >= a[pivot1])
  253.         {
  254.             swap(a, i, pivot1);
  255.             pivot1++;
  256.             if(i != pivot1)
  257.                 swap(a, i, pivot1);
  258.         }
  259.         else if(a[i] <= a[pivot2])
  260.         {
  261.             swap(a, i, pivot2);
  262.             pivot2--;
  263.             if(i != pivot2)
  264.                 swap(a, i, pivot2);
  265.             i--;
  266.         }
  267.     }
  268.     dualpivot(a, pivot1);
  269.     dualpivot(a + pivot1, pivot2 - pivot1);
  270.     dualpivot(a + pivot2, n - pivot2);
  271. }
  272.  
  273. int main()
  274. {
  275.     int i, n, choice;
  276.     int *a;
  277.     cout<<"\nNumber of elements? ";
  278.     cin>>n;
  279.     a = new int[n];
  280.     cout<<"\nEnter "<<n<<" numbers:\n";
  281.     for(i=0; i < n; i++)
  282.         cin>>a[i];
  283.     cout<<"\nChoose a sorting method:\n0. Simple sort\n1. Bubblesort\n2. Mergesort";
  284.     cout<<"\n3. Quicksort\n4. Selection sort\n5. Insertion sort\n6. Heapsort";
  285.     cout<<"\n7. Count sort\n8. Bucket sort\n9. Radix sort\n10. Dual pivot quicksort\n\n";
  286.     cin>>choice;
  287.     switch(choice)
  288.     {
  289.         case 0: simplesort(a,n); break;
  290.         case 1: bubblesort(a,n); break;
  291.         case 2: mergesort(a,n); break;
  292.         case 3: quicksort(a,n); break;
  293.         case 4: selectsort(a,n); break;
  294.         case 5: insertsort(a,n); break;
  295.         case 6: heapsort(a,n); break;
  296.         case 7: countsort(a,n); break;
  297.         case 8: bucketsort(a,n); break;
  298.         case 9: radixsort(a,n); break;
  299.         case 10: dualpivot(a,n); break;
  300.         default: ;
  301.     }
  302.     cout<<"\nThe sorted array is:\n";
  303.     for(i=0; i < n; i++)
  304.         cout<<a[i]<<" ";
  305.     cout<<"\n";
  306.     return 0;
  307. }
  308.  
Advertisement
Add Comment
Please, Sign In to add comment