-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathquick_sort.cpp
28 lines (24 loc) · 1.1 KB
/
quick_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
using std::rand;
using std::swap;
// pivot - "опорный" элемент
// partition - переупорядочивает элементы части массива,
// заданной отрезком [left, right), так что в начале
// следуют элементы меньшие pivot, а в конце - большие;
// возвращает место начала блока элементов, больших pivot;
int * partition(int * left, int * right, int pivot) {
int * store = left; // место для вставки элементов, меньших pivot
for (int * p = left; p != right; ++p)
if (*p < pivot)
swap(*p, *store++);
return store;
}
void my_qsort(int * arr, int n) {
if (n <= 1)
return; // массив в 1 или 0 элементов уже упорядочен
int * pivotPtr = arr + rand() % n; // случайный выбор опорного элемента
int newPivotIdx = partition(arr, arr + n, *pivotPtr) - arr;
my_qsort(arr, newPivotIdx + 1);
my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1));
}