Skip to content

Commit 8790487

Browse files
Add quick_sort.cpp
1 parent bcfb51b commit 8790487

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

sorting/quick_sort.cpp

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

0 commit comments

Comments
 (0)