-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathquicksort.cpp
More file actions
61 lines (55 loc) · 838 Bytes
/
quicksort.cpp
File metadata and controls
61 lines (55 loc) · 838 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
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
int part(int low, int high, int arr[])
{
int i = low;
int j = high;
int pivot = arr[i];
while(i<j)
{
do{
i++;
}while(arr[i]<=pivot);
do{
j--;
}while(arr[j]>pivot);
if(i<j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
return j;
}
void quicksort(int high, int low, int arr[])
{
if(low<high)
{
int j = part(low, high,arr);
quicksort(j,low,arr);
quicksort(high, j+1,arr);
}
}
int main()
{
int n;
cout<<"Enter the number of elements in the array : ";
cin>>n;
int arr[n];
int i;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
quicksort(n,0,arr);
i =0;
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
// cout<<endl;
}