Skip to content

Commit a378856

Browse files
authored
Merge pull request #31 from Codemon06/new-program
Quick-Sort Program
2 parents c8752c0 + b993055 commit a378856

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

quick-sort.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
int partition(int a[], int l, int h){
6+
int pivot, i, j, temp;
7+
pivot = a[h];
8+
i = l-1;
9+
for(j=l; j<h; j++){
10+
if(a[j] <= pivot){
11+
i = i+1;
12+
temp = a[i];
13+
a[i] = a[j];
14+
a[j] = temp;
15+
}
16+
}
17+
temp = a[i+1];
18+
a[i+1] = a[h];
19+
a[h] = temp;
20+
return i+1;
21+
}
22+
23+
void quicksort(int a[], int l, int h){
24+
if(l<h){
25+
int q;
26+
q = partition(a, l, h);
27+
quicksort(a, l, q-1);
28+
quicksort(a, q+1, h);
29+
}
30+
}
31+
32+
33+
int main(){
34+
int i, n;
35+
cout<<"Enter the number of elements: ";
36+
cin>>n;
37+
int a[n];
38+
cout<<"Enter the elements: ";
39+
for(i=0; i<n; i++)
40+
cin>>a[i];
41+
quicksort(a, 0, n-1);
42+
cout<<"The sorted array is: ";
43+
for(i=0; i<n; i++)
44+
cout<<a[i]<<" ";
45+
return 0;
46+
}

0 commit comments

Comments
 (0)