-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
67 lines (62 loc) · 1.47 KB
/
QuickSort.cpp
File metadata and controls
67 lines (62 loc) · 1.47 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*Quick sort implementation.
In this method we select a pivot and all the elements smaller than the pivot are moved to left of the pivot
and all the elemets greater than the pivot to the right.
Complexity is O(n^2) in worst case but in average case its O(n log n).The worst case behaviour is rare.
Quick sort is an partitioning inplace algorithm.
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int partition(vector<int>& v, int start, int end)
{
int pivot = v[end];
int pIndex = start;
for (int i = start; i < end; ++i)
{
if (v[i] <= pivot) //Arrange the elements accordingly.
{
swap(v[i], v[pIndex]);
++pIndex;
}
}
swap(v[pIndex], v[end]); //swap the pivot and element at partition index.
return pIndex;
}
void quickSort(vector<int>& v, int start, int end)
{
if (start < end){
int pIndex = partition(v, start, end);
//recursively call the function.
quickSort(v, start, pIndex - 1);
quickSort(v, pIndex + 1, end);
}
}
int main()
{
srand(time(0));
vector<int> v;
int insert;
int count = 0;
while (count<8)//randomly generate and fill the vector with unique numbers
{
insert = rand() % 15;
if (find(v.begin(), v.end(), insert) == v.end())
{
v.push_back(insert);
++count;
}
}
cout << "Before sort: ";
for (auto i : v)
cout << i << " ";
quickSort(v,0,7);
cout << endl;
cout << "After sort: ";
for (auto i : v)
cout << i << " ";
system("pause");
}