Skip to content

Commit 25f55c2

Browse files
authored
Merge pull request AllAlgorithms#10 from underscoreorcus/master
Added quick_sort.cpp
2 parents 5c241f9 + 0d0d2e2 commit 25f55c2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

sorting/quick_sort.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
void quickSort(vector<int>&,int,int);
6+
int partition(vector<int>&, int,int);
7+
8+
int main()
9+
{
10+
vector<int> A = {10,9,8,7,6,5,4,3,2,1};
11+
int start = 0;
12+
int end = (int)A.size();
13+
cout << "Before:" << endl;
14+
for(auto value : A)
15+
cout << value <<" ";
16+
cout << endl;
17+
quickSort(A, start, end);
18+
cout << "After: " << endl;
19+
for(auto value : A)
20+
cout << value <<" ";
21+
cout << endl;
22+
}
23+
24+
void quickSort(vector<int>& A, int start,int end)
25+
{
26+
int pivot;
27+
if(start < end)
28+
{
29+
pivot=partition(A, start, end);
30+
quickSort(A, start, pivot);
31+
quickSort(A, pivot+1, end);
32+
}
33+
}
34+
35+
36+
int partition(vector<int>& A, int start,int end)
37+
{
38+
int x = A[start];
39+
int i = start;
40+
int j;
41+
for(j = start+1; j < end; j++)
42+
{
43+
if(A[j]<=x)
44+
{
45+
i=i+1;
46+
swap(A[i],A[j]);
47+
}
48+
}
49+
swap(A[i],A[start]);
50+
return i;
51+
}

0 commit comments

Comments
 (0)