Advertisement
Guest User

quick sort

a guest
May 30th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void Swap(int Element[], constint i, constint j)
  5. {
  6.     Int temp = Element[i];
  7.     Element[i] = Element[j];
  8.     Element[j] = temp;
  9. }
  10.  
  11. int Partition(int Element[], const int low, const int high)
  12. {
  13.     Int pivotloc, i;
  14.     Int pivotkey;
  15.  
  16.     pivotkey = Element[low];
  17.     pivotloc = low;
  18.     for (i = low + 1; i <= high; i++)
  19.         if (Element[i] < pivotkey)
  20.             Swap(Element,++pivotloc, i);
  21.  
  22.     Swap(Element,low, pivotloc);
  23.     return pivotloc;
  24. }
  25.  
  26.  
  27. void QuickSort(int Element[],const int low, const int high)  
  28. {
  29.     int pivotloc;
  30.  
  31.     if (low<high)
  32.     {
  33.         pivotloc = Partition(Element,low, high);
  34.         QuickSort(Element,low, pivotloc - 1);
  35.         QuickSort(Element,pivotloc + 1, high);
  36.     }
  37. }
  38.  
  39. void PRINT(int A[], int n)
  40. {
  41.     for (int i = 0; i<n; i++)
  42.     {
  43.         cout<< A[i] <<endl;
  44.     }
  45. }
  46.  
  47. void main()
  48. {
  49.     int A[]={24,13,6,75,9,63,19,29,8,1,5,76,82,11,55,45,60,50,32,25};
  50.     int brE = 20;
  51.  
  52.     cout<<"Unsorted array:"<<endl;
  53.     PRINT(A, brE);
  54.    
  55.     QuickSort(A,0,19);
  56.    
  57.     cout<<endl<<"Sorted array"<<endl;
  58.     PRINT(A, brE);
  59.  
  60.     system("pause");
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement