-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
45 lines (39 loc) · 881 Bytes
/
quicksort.cpp
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
#include<iostream>
#include<algorithm>
using namespace std;
int partitonvalue(int arr[],int low,int high){
int puppet=arr[low];
int i=low;
int j=high;
while (i<j)
{
while (arr[i]<=puppet&&i<high)
{
i++;
}
while (arr[j]>puppet&&j>=low+1)
{
j--;
}
if(arr[i]>arr[j])swap(arr[i],arr[j]);
swap(arr[low],arr[j]);
}
return j;
}
void quicksort(int arr[],int low,int high){
if(low<high){
int partition=partitonvalue(arr,low,high);
//left portion
quicksort(arr,low,partition-1);
quicksort(arr,partition+1,high);
}
}
int main(){
int arr[5]={5,9,7,1,2};
quicksort(arr,0,4);
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}