Advertisement
Mancolo

Быстрая сортировка

Oct 15th, 2022
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. using namespace std;
  4.  
  5. int partition(int list[], int start, int pivot)
  6. {
  7.     int i = start;
  8.     while (i < pivot)
  9.     {
  10.         if (list[i] > list[pivot] && i == pivot - 1)
  11.         {
  12.             swap(list[i], list[pivot]);
  13.             pivot--;
  14.         }
  15.  
  16.         else if (list[i] > list[pivot])
  17.         {
  18.             swap(list[pivot - 1], list[pivot]);
  19.             swap(list[i], list[pivot]);
  20.             pivot--;
  21.         }
  22.  
  23.         else i++;
  24.     }
  25.     return pivot;
  26. }
  27.  
  28. void quickSort(int list[], int start, int end)
  29. {
  30.     if (start < end)
  31.     {
  32.         int pivot = partition(list, start, end);
  33.  
  34.         quickSort(list, start, pivot - 1);
  35.         quickSort(list, pivot + 1, end);
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     int list[6] = { 2, 12, 5, 48, 0, 4 };
  42.     cout << "Input array ...\n";
  43.     for (int i = 0; i < 6; i++)
  44.     {
  45.         cout << list[i] << "\t";
  46.     }
  47.  
  48.     quickSort(list, 0, 6);
  49.  
  50.     cout << "\n\nSorted array ... \n";
  51.     for (int i = 0; i < 6; i++)
  52.     {
  53.         cout << list[i] << "\t";
  54.     }
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement