Advertisement
Guest User

Untitled

a guest
May 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void quickSort(int a[], int first, int last);
  4. int pivot(int a[], int first, int last);
  5. void swap(int& a, int& b);
  6. void swapNoTemp(int& a, int& b);
  7. void print(int array[], const int& N);
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. int test[100];
  14. int n;
  15. cin>>n;
  16. for(int i=0; i<n; i++)
  17. {
  18. cin>>test[i];
  19. }
  20. cout << "Size of test array :"<<endl;
  21. for(int i=0; i<n; i++)
  22. {
  23. cout<<test[i];
  24. }
  25. cout << "Before sorting : " << endl;
  26. print(test, n);
  27.  
  28. quickSort(test, 0, n-1);
  29.  
  30. cout << endl << endl << "After sorting : " << endl;
  31. print(test, n);
  32.  
  33. return 0;
  34. }
  35.  
  36. /**
  37. * Quicksort.
  38. * @param a - The array to be sorted.
  39. * @param first - The start of the sequence to be sorted.
  40. * @param last - The end of the sequence to be sorted.
  41. */
  42. void quickSort( int a[], int first, int last )
  43. {
  44. int pivotElement;
  45.  
  46. if(first < last)
  47. {
  48. pivotElement = pivot(a, first, last);
  49. quickSort(a, first, pivotElement-1);
  50. quickSort(a, pivotElement+1, last);
  51. }
  52. }
  53.  
  54. /**
  55. * Find and return the index of pivot element.
  56. * @param a - The array.
  57. * @param first - The start of the sequence.
  58. * @param last - The end of the sequence.
  59. * @return - the pivot element
  60. */
  61. int pivot(int a[], int first, int last)
  62. {
  63. int p = first;
  64. int pivotElement = a[first];
  65.  
  66. for(int i = first+1 ; i <= last ; i++)
  67. {
  68. /* If you want to sort the list in the other order, change "<=" to ">" */
  69. if(a[i] <= pivotElement)
  70. {
  71. p++;
  72. swap(a[i], a[p]);
  73. }
  74. }
  75.  
  76. swap(a[p], a[first]);
  77.  
  78. return p;
  79. }
  80.  
  81.  
  82. /**
  83. * Swap the parameters.
  84. * @param a - The first parameter.
  85. * @param b - The second parameter.
  86. */
  87. void swap(int& a, int& b)
  88. {
  89. int temp = a;
  90. a = b;
  91. b = temp;
  92. }
  93.  
  94. /**
  95. * Swap the parameters without a temp variable.
  96. * Warning! Prone to overflow/underflow.
  97. * @param a - The first parameter.
  98. * @param b - The second parameter.
  99. */
  100. void swapNoTemp(int& a, int& b)
  101. {
  102. a -= b;
  103. b += a;// b gets the original value of a
  104. a = (b - a);// a gets the original value of b
  105. }
  106.  
  107. /**
  108. * Print an array.
  109. * @param a - The array.
  110. * @param N - The size of the array.
  111. */
  112. void print(int a[], const int& N)
  113. {
  114. for(int i = 0 ; i < N ; i++)
  115. cout << "array[" << i << "] = " << a[i] << endl;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement