-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquicksort.cpp
81 lines (75 loc) · 1.55 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//quicksort
//divide and conquer
//clrs pg 170
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<ctype.h>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
#define inf 1000000000
#define mod 1000000007
#define ll long long
#define in(x) scanf("%d",&x);
#define rep(i,n) for(i=0;i<n;i++)
#define rrep(i,n) for(i=n-1;i>=0;i--)
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
const double pi(3.14159265358979);
// logic:
// for subarray a[p...r] position q is such that all elements at p<=i<q are less than a[q] and q>i>=r are greater than a[q]
// keeping a[r] as pivot element...it can be someone else also..we sort such that a[q] becomes a[r] after swapping
// and then sort both subarrays recursively
// before every loop ... line 45
// elements b/w p and i are less than or equal to x
// b/w i+1 nd j-1 are more than x
// b/w j nd r are unsorted
int a[10000];
int part(int p,int r)
{
int x=a[r],i,j;
i=p-1;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i++;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[r]);
cout<<p<<" "<<r<<" "<<i+1<<endl;
return i+1;
}
int quick(int p,int r)
{
if(p<r)
{
int q=part(p,r);
quick(p,q-1);
quick(q+1,r);
}
}
// take example
// 2 8 7 1 3 5 6 4
int main()
{
int n,i;
in(n)
rep(i,n)
in(a[i])
quick(0,n-1);
rep(i,n)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}