-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick.cpp
More file actions
37 lines (35 loc) · 778 Bytes
/
Copy pathquick.cpp
File metadata and controls
37 lines (35 loc) · 778 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
#include <bits/stdc++.h>
using namespace std;
int partition(int arr[], int p, int r)
{
int povit = arr[r];
int i = p-1;
for(int j = p; j <= r-1; j++){
if(arr[j] >= povit) {
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[r]);
return i+1;
}
int quick(int arr[], int p, int r)
{
if(p < r){
int q = partition(arr,p,r);
quick(arr,p,q-1);
quick(arr,q+1,r);
}
}
int main()
{
int arr[100],n;
cout<<"Enter the array size: ";
cin>>n;
cout<<"Enter the array: ";
for(int i = 1; i <= n; i++) cin>>arr[i];
quick(arr,1,n);
cout<<"The sorted array is: \n";
for(int i = 1; i <= n; i++) cout<<arr[i] <<"\t";
return 0;
}