-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
49 lines (48 loc) · 1.29 KB
/
QuickSort.cpp
File metadata and controls
49 lines (48 loc) · 1.29 KB
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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Disadvantages:
// 1. It's non adaptive, and it's the worse case, with time complexity O(n^2)
// 2. It's not stable , means it don't maintain the order of output as it was in input
class QuickSort
{
public:
size_t partition(vector<int> &arr, size_t low, size_t high)
{
int pivot{arr.at(high)};
size_t i{low - 1};
for (size_t j = low; j < high; j++)
{
if (arr.at(j) <= pivot)
{
i++;
swap(arr.at(i), arr.at(j));
}
}
swap(arr.at(high), arr.at(i + 1));
return i + 1;
}
void sort(vector<int> &arr, size_t low, size_t high) // time complexity is O(nlog n)
{
if (low < high)
{
size_t partitionIndex = partition(arr, low, high);
sort(arr, low, partitionIndex - 1); // sorting left side
sort(arr, partitionIndex + 1, high); // sorting right side
}
}
};
int main()
{
vector<int> arr{5, 0, 9, 4, 8, 4, 3};
QuickSort sorting;
sorting.sort(arr, 0, arr.size() - 1);
cout << "[";
for (auto i : arr)
{
cout << i << " ";
}
cout << "]" << endl;
return 0;
}