Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. int partition (int arr[], int l, int h)
  5. {
  6. int x = arr[h];
  7. int i = (l - 1);
  8.  
  9. for (int j = l; j <= h- 1; j++)
  10. {
  11. if (arr[j] <= x)
  12. {
  13. i++;
  14. std::swap(arr[i], arr[j]);
  15. }
  16. }
  17. std::swap(arr[i + 1], arr[h]);
  18. return (i + 1);
  19. }
  20.  
  21. /* A[] --> Array to be sorted, l --> Starting index, h --> Ending index */
  22. void quickSort(int A[], int l, int h)
  23. {
  24. if (l < h)
  25. {
  26. int p = partition(A, l, h); /* Partitioning index */
  27. quickSort(A, l, p - 1);
  28. quickSort(A, p + 1, h);
  29. }
  30.  
  31. }
  32.  
  33. int main()
  34. {
  35. int N;
  36. int l = 1, h = N-1;
  37. int i = 0;
  38. int array[N];
  39. std::cin >> N;
  40. for( int j = 0; j<N; j++)
  41. {
  42. std::cin >> array[j];
  43. j++;
  44. }
  45. quickSort(array, l, h);
  46. for( int k = 0; k<N; k++)
  47. {
  48. std::cout << array[k];
  49. }
  50.  
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement