Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- int partition (int arr[], int l, int h)
- {
- int x = arr[h];
- int i = (l - 1);
- for (int j = l; j <= h- 1; j++)
- {
- if (arr[j] <= x)
- {
- i++;
- std::swap(arr[i], arr[j]);
- }
- }
- std::swap(arr[i + 1], arr[h]);
- return (i + 1);
- }
- /* A[] --> Array to be sorted, l --> Starting index, h --> Ending index */
- void quickSort(int A[], int l, int h)
- {
- if (l < h)
- {
- int p = partition(A, l, h); /* Partitioning index */
- quickSort(A, l, p - 1);
- quickSort(A, p + 1, h);
- }
- }
- int main()
- {
- int N;
- int l = 1, h = N-1;
- int i = 0;
- int array[N];
- std::cin >> N;
- for( int j = 0; j<N; j++)
- {
- std::cin >> array[j];
- j++;
- }
- quickSort(array, l, h);
- for( int k = 0; k<N; k++)
- {
- std::cout << array[k];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement