Advertisement
wintest

QUICKSORT С принтиране

Nov 5th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define size 5
  5.  
  6. void quickSort(int arr[], int left, int right){
  7.     cout << "Quicksort of array : " << endl;
  8.     for (size_t k = left; k <= right; k++)
  9.     {
  10.         cout << arr[k] << " ";
  11.     }
  12.     cout << endl;
  13.     int i = left;
  14.     cout << "I is " << i << endl;
  15.     int j = right;
  16.     cout << "J is " << j << endl;
  17.     int tmp;
  18.     int pivot = arr[(left + right) / 2];
  19.     cout << "Pivot is " << pivot << endl;
  20.     while (i <= j){
  21.         while (arr[i] < pivot){
  22.             cout << "Element " << arr[i] << " is smaller than pivot! Not touching!" << endl;
  23.             i++;
  24.         }
  25.         while (arr[j] > pivot){
  26.             cout << "Element " << arr[j] << " is smaller than pivot! Not touching!" << endl;
  27.             j--;
  28.         }
  29.         if (i <= j){
  30.             cout << "I shall swap " << arr[i] << " with " << arr[j] << endl;
  31.             tmp = arr[i];
  32.             arr[i] = arr[j];
  33.             arr[j] = tmp;
  34.             i++;
  35.             j--;
  36.             cout << "The swapped array looks like this: " << endl;
  37.             for (size_t k = 0; k < size; k++)
  38.             {
  39.                 cout << arr[k] << " ";
  40.             }
  41.             cout << endl;
  42.             cout << endl;
  43.         }
  44.     }
  45.     if (left < j){
  46.         cout << "Calling quicksort for array from element " << left << " to element " << j << endl;
  47.         cout << "Going down!" << endl;
  48.         quickSort(arr, left, j);
  49.         cout << "Going up!" << endl;
  50.     }
  51.     if (right > i){
  52.         cout << "Calling quicksort for array from element " << i << " to element " << right << endl;
  53.         cout << "Going down!" << endl;
  54.         quickSort(arr, i, right);
  55.         cout << "Going up!" << endl;
  56.     }
  57.  
  58. }
  59.  
  60. int main(){
  61.     int arr[] = {5,4,1,4,8 };
  62.     quickSort(arr, 0, 4);
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement