Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee33695

Browse files
Add files via upload
1 parent 4cb60e3 commit ee33695

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎Sorting/Quick Sort/quick_sort.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long int
4+
#define loop(i,N) for(int i=0;i<N;i++)
5+
#define loop_(i,N) for(int i=1;i<=N;i++)
6+
7+
vector <int> ar;
8+
9+
int partition(int low,int high){
10+
int pivot=ar[high];
11+
int wall=low-1,temp;
12+
while(low<high){
13+
if(ar[low]<=pivot){
14+
++wall;
15+
temp=ar[wall];
16+
ar[wall]=ar[low];
17+
ar[low]=temp;
18+
}
19+
++low;
20+
}
21+
wall++;
22+
temp=ar[wall];
23+
ar[wall]=pivot;
24+
ar[high]=temp;
25+
return wall;
26+
}
27+
28+
void quick_sort(int low,int high){
29+
if(low<high){
30+
int wall=partition(low,high);
31+
quick_sort(low,wall-1);
32+
quick_sort(wall+1,high);
33+
}
34+
}
35+
36+
int main(){
37+
int num;
38+
int n;
39+
cout<<"Enter no. of elements to be sorted: ";
40+
cin>>n;
41+
cout<<"\nEnter elements: ";
42+
loop(i,n) cin>>num, ar.push_back(num);
43+
quick_sort(0,ar.size()-1);
44+
cout<<"\nSorted: ";
45+
loop(i,n) cout<<ar[i]<<" ";
46+
cout<<endl;
47+
return 0;
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /