-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
48 lines (41 loc) · 918 Bytes
/
quick_sort.cpp
File metadata and controls
48 lines (41 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
void print(int arr[],int j,int n){
cout << "The elements of the array are :" ;
for(int i = j;i<n;i++){
cout << arr[i] << " " ;
}
cout << endl;
}
int partition(int arr[],int l, int r){
int piv = arr[r];
int tr = l;
for(int i=l;i<=r;i++){
if(arr[i] <= piv){
int temp = arr[i];
arr[i] = arr[tr];
arr[tr] = temp;
tr++;
}
}
return tr-1;
}
void quick_sort(int arr[], int l, int r){
if(l>=r){return;}
int p = partition(arr, l, r);
quick_sort(arr,l,p-1);
quick_sort(arr,p+1,r);
}
int main(){
int n;
cout << "Enter the number of elements in the list..??" << endl;
cin >> n;
int ar[n];
for(int i=0;i<n;i++){
cout << "Enter it: ";
cin >> ar[i];
}
int* arr = ar;
quick_sort(arr,0,n-1);
print(arr,0,n);
}