-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort_057.cpp
More file actions
50 lines (50 loc) · 832 Bytes
/
Copy pathquickSort_057.cpp
File metadata and controls
50 lines (50 loc) · 832 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
49
50
#include<iostream>
using namespace std;
void Swap(int arr[], int s, int e)
{
int temp=arr[s];
arr[s]=arr[e];
arr[e]=temp;
}
int Partition(int arr[], int s, int e)
{
int pivot= arr[s];
int i=s,j=e;
while(i<j)
{
do
{
i++;
}
while(arr[i]<=pivot);
do
{
j--;
}
while(arr[j]>pivot);
if(i<j)
Swap(arr,i,j);
}
Swap(arr,s,j);
return j;
}
void quicksort(int arr[],int s, int e)
{
if(s<e)
{
int pivot=Partition(arr,s,e);
quicksort(arr,s,pivot);
quicksort(arr,pivot+1,e);
}
}
int main()
{
int arr[]={3,5,2,6,4,7};
int n=sizeof(arr)/sizeof(arr[0]);
quicksort(arr,0,n-1);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}