forked from happy522/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate QuickSort.cpp
More file actions
70 lines (58 loc) · 1.2 KB
/
Create QuickSort.cpp
File metadata and controls
70 lines (58 loc) · 1.2 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long;
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define cases(t) int t;cin>>t;while(t--)
#define endl "\n"
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define fo(i,n) for( i=0;i<n;i++)
#define largest 1e20
typedef vector<int> VI;
void display(VI arr);
void swap(VI &,int ,int);
void quicksort(VI &a,int start,int end)
{
int pivot=a[start];
int start_index=start;
int end_index=end;
start++;
if(start<=end)
{
while(start<=end)
{
while(a[start]<pivot&&start<=end)
start++;
while(a[end]>pivot && start<=end)
end--;
start<end ? swap(a,start,end) : swap(a,start_index,end);
}
}
else return;
quicksort(a,start_index,end-1);
quicksort(a,end+1,end_index);
}
void solve()
{///
VI arr={9,8,7,6,5,4,3,2,1};
display(arr);
quicksort(arr,0,8);
display(arr);
}
int main()
{
solve();
}
void display(VI arr)
{
for(int i=0;i<arr.size();i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void swap(VI &arr,int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}