-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
46 lines (37 loc) · 759 Bytes
/
quick_sort.cpp
File metadata and controls
46 lines (37 loc) · 759 Bytes
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
#include<bits/stdc++.h>
int partition(int arr[], int l, int h);
void swap(int *x, int *y);
void quick_sort(int arr[], int l, int h);
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
void quick_sort(int arr[], int l, int h) {
if(l < h){
int pi = partition(arr, l, h);
quick_sort(arr, l, pi-1);
quick_sort(arr, pi+1, h);
}
}
int partition(int arr[], int l, int h){
int pivot = arr[h];
int i = l-1;
for (int j = l; j <= h-1; j++){
if (arr[j] <= pivot){
i += 1;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[h]);
return i+1;
}
int main(){
std::ios_base::sync_with_stdio(false);
int arr[] = {10, 80, 30, 90, 40, 50, 70};
quick_sort(arr, 0, 6);
for(int i=0;i<7;i++){
printf("%d ", arr[i]);
}
return 0;
}