-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec36_rec6.cpp
81 lines (64 loc) · 1.22 KB
/
lec36_rec6.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
//Quick sort
#include<bits/stdc++.h>
using namespace std;
void printarr(int arr[], int s, int e)
{
for (int i = s; i <= e; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int partition(int arr[],int s,int e)
{
int pivot=arr[s];
int count =0;
for(int i=s+1;i<=e;i++)
{
if(arr[i]<pivot)
count++;
}
int pivotindex= s + count;
swap(arr[pivotindex],arr[s]);
//left and right wala part sambhaal lete hy
int i =s,j=e;
while(i<pivotindex && j>pivotindex)
{
while(arr[i]<=pivot)
{
i++;
}
while(arr[j]>pivot)
{
j--;
}
if(i<pivotindex && j>pivotindex)
{
swap(arr[i++],arr[j--]);
}
}
return pivotindex;
}
void quicksort(int arr[],int s,int e)
{
//base case
if(s>e)
return ;
//partition karenge
int p = partition(arr,s,e);
//left part sort
quicksort(arr,s,p-1);
//right part
quicksort(arr,p+1,e);
}
int main()
{
int arr[5]={2,4,1,6,9};
int n=5;
int arr2[6]={3,1,4,5,2,0};
int x=6;
quicksort(arr,0,n-1);
printarr(arr,0,n-1);
quicksort(arr2,0,x-1);
printarr(arr2,0,x-1);
}