Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #define size 5
- void quickSort(int arr[], int left, int right){
- cout << "Quicksort of array : " << endl;
- for (size_t k = left; k <= right; k++)
- {
- cout << arr[k] << " ";
- }
- cout << endl;
- int i = left;
- cout << "I is " << i << endl;
- int j = right;
- cout << "J is " << j << endl;
- int tmp;
- int pivot = arr[(left + right) / 2];
- cout << "Pivot is " << pivot << endl;
- while (i <= j){
- while (arr[i] < pivot){
- cout << "Element " << arr[i] << " is smaller than pivot! Not touching!" << endl;
- i++;
- }
- while (arr[j] > pivot){
- cout << "Element " << arr[j] << " is smaller than pivot! Not touching!" << endl;
- j--;
- }
- if (i <= j){
- cout << "I shall swap " << arr[i] << " with " << arr[j] << endl;
- tmp = arr[i];
- arr[i] = arr[j];
- arr[j] = tmp;
- i++;
- j--;
- cout << "The swapped array looks like this: " << endl;
- for (size_t k = 0; k < size; k++)
- {
- cout << arr[k] << " ";
- }
- cout << endl;
- cout << endl;
- }
- }
- if (left < j){
- cout << "Calling quicksort for array from element " << left << " to element " << j << endl;
- cout << "Going down!" << endl;
- quickSort(arr, left, j);
- cout << "Going up!" << endl;
- }
- if (right > i){
- cout << "Calling quicksort for array from element " << i << " to element " << right << endl;
- cout << "Going down!" << endl;
- quickSort(arr, i, right);
- cout << "Going up!" << endl;
- }
- }
- int main(){
- int arr[] = {5,4,1,4,8 };
- quickSort(arr, 0, 4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement