Skip to content

Commit b9ef111

Browse files
authored
Merge pull request kothariji#448 from samprati97/master
Create Quick Sort.cpp
2 parents a779f62 + ecec926 commit b9ef111

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Sorting/Quick Sort.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# include <iostream>
2+
using namespace std;
3+
// quick sort sorting algorithm
4+
int Partition(int arr[], int s, int e)
5+
{
6+
int pivot = arr[e];
7+
int pIndex = s;
8+
9+
for(int i = s;i<e;i++)
10+
{
11+
if(arr[i]<pivot)
12+
{
13+
int temp = arr[i];
14+
arr[i] = arr[pIndex];
15+
arr[pIndex] = temp;
16+
pIndex++;
17+
}
18+
}
19+
20+
int temp = arr[e];
21+
arr[e] = arr[pIndex];
22+
arr[pIndex] = temp;
23+
24+
return pIndex;
25+
}
26+
27+
void QuickSort(int arr[], int s, int e)
28+
{
29+
if(s<e)
30+
{
31+
int p = Partition(arr,s, e);
32+
QuickSort(arr, s, (p-1)); // recursive QS call for left partition
33+
QuickSort(arr, (p+1), e); // recursive QS call for right partition
34+
}
35+
}
36+
37+
int main()
38+
{
39+
40+
int size=0;
41+
cout<<"Enter Size of array: "<<endl;
42+
cin>>size;
43+
int myarray[size];
44+
45+
cout<<"Enter "<<size<<" integers in any order: "<<endl;
46+
for(int i=0;i<size;i++)
47+
{
48+
cin>>myarray[i];
49+
}
50+
cout<<"Before Sorting"<<endl;
51+
for(int i=0;i<size;i++)
52+
{
53+
cout<<myarray[i]<<" ";
54+
}
55+
cout<<endl;
56+
57+
QuickSort(myarray,0,(size-1)); // quick sort called
58+
59+
cout<<"After Sorting"<<endl;
60+
for(int i=0;i<size;i++)
61+
{
62+
cout<<myarray[i]<<" ";
63+
}
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)