forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
51 lines (46 loc) · 969 Bytes
/
quick_sort.cpp
File metadata and controls
51 lines (46 loc) · 969 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
void quickSort(vector<int>&,int,int);
int partition(vector<int>&, int,int);
int main()
{
vector<int> A = {10,9,8,7,6,5,4,3,2,1};
int start = 0;
int end = (int)A.size();
cout << "Before:" << endl;
for(auto value : A)
cout << value <<" ";
cout << endl;
quickSort(A, start, end);
cout << "After: " << endl;
for(auto value : A)
cout << value <<" ";
cout << endl;
}
void quickSort(vector<int>& A, int start,int end)
{
int pivot;
if(start < end)
{
pivot=partition(A, start, end);
quickSort(A, start, pivot);
quickSort(A, pivot+1, end);
}
}
int partition(vector<int>& A, int start,int end)
{
int x = A[start];
int i = start;
int j;
for(j = start+1; j < end; j++)
{
if(A[j]<=x)
{
i=i+1;
swap(A[i],A[j]);
}
}
swap(A[i],A[start]);
return i;
}