-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick Sort.cpp
53 lines (47 loc) · 1.03 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
int* quickSort(int* arr, const int& arrSz)
{
if (arrSz < 2)
return arr;
int propElem = arr[0];
size_t lessNum = 0;
size_t greaterNum = 0;
for (size_t i = 1; i != arrSz; ++i)
{
if (arr[i] <= propElem)
++lessNum;
else
++greaterNum;
}
int* less = new int[lessNum];
int* greater = new int[greaterNum];
for (size_t i = 1, lessCntr = 0, greaterCntr = 0; i < arrSz; i++)
{
if (arr[i] <= propElem)
less[lessCntr++] = arr[i];
else
greater[greaterCntr++] = arr[i];
}
quickSort(less, lessNum);
quickSort(greater, greaterNum);
size_t tempI = 0;
for (; tempI < lessNum; ++tempI)
{
arr[tempI] = less[tempI];
}
arr[tempI++] = propElem;
for (size_t i = 0; tempI < arrSz; ++tempI, ++i)
{
arr[tempI] = greater[i];
}
return arr;
}
int main()
{
int arr[] = { 2,1,5,7,9};
int arrSize = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, arrSize);
std::cout << "Sorted array is \n";
for (int i = 0; i < arrSize; ++i)
std::cout << arr[i] << " ";
}