-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick Sort || O(nlogn) Time complexity || O(1) space Complexity |
83 lines (78 loc) · 1.58 KB
/
Quick Sort || O(nlogn) Time complexity || O(1) space Complexity |
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
82
83
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
//Ascending Order ||
int find(int arr[] , int low, int high){
int i = low;
int j = high;
int pivot = arr[low];
while(i<j){
while(pivot>=arr[i] && i<=high){
i++;
}
while(pivot<arr[j] && j>=low){
j--;
}
if(i<j){
swap(arr[i],arr[j]);
}
}
swap(arr[low],arr[j]);
return j;
}
void QS(int arr[],int low ,int high){
if(low<high){
int partition = find(arr,low,high);
QS(arr,low,partition-1);
QS(arr,partition+1,high);
}
}
int main(){
int arr[]={5,100,3,56,0,1};
int size = 6;
QS(arr,0,size-1);
for(int i=0; i<7; i++){
cout<<arr[i]<<" ";
}
return 0;
____________________________________________________________________________________________________________
____________________________________________________________________________________________________________
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
//Ascending Order ||
int find(int arr[] , int low, int high){
int i = low;
int j = high;
int pivot = arr[low];
while(i<j){
while(pivot<=arr[i] && i<=high){
i++;
}
while(pivot>arr[j] && j>=low){
j--;
}
if(i<j){
swap(arr[i],arr[j]);
}
}
swap(arr[low],arr[j]);
return j;
}
void QS(int arr[],int low ,int high){
if(low<high){
int partition = find(arr,low,high);
QS(arr,low,partition-1);
QS(arr,partition+1,high);
}
}
int main(){
int arr[]={5,100,3,56,0,1};
int size = 6;
QS(arr,0,size-1);
for(int i=0; i<7; i++){
cout<<arr[i]<<" ";
}
return 0;