-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.cpp
62 lines (48 loc) · 921 Bytes
/
quick_sort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin);
#define write() freopen("output.txt", "w", stdout);
#define ll long long int
int Part(int arr[],int low,int high)
{
int pivot=arr[high],i,j,t,k;
k=low-1;
for( i=low;i<high;i++)
{
if(arr[i]<pivot)
{
k++;
swap(arr[i],arr[k]);
}
}
swap(arr[k+1],arr[high]);
return k+1;
}
void QuickSort(int arr[],int low,int high)
{
if(low<high)
{
int p=Part(arr,low,high);
QuickSort(arr,low,p-1);
QuickSort(arr,p+1,high);
}
}
int main()
{
#ifdef asiuzzaman
read(); write();
#endif
int n;cin>>n;
int arr[n];
for (int i = 0; i < n; ++i)
{
cin>>arr[i];
}
int low=0,high=n-1;
QuickSort(arr,low,high);
for (int i = 0; i < n; ++i)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}