-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.c
48 lines (40 loc) · 855 Bytes
/
Quick_Sort.c
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
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int l, int h) {
int k = array[h];
int i = (l - 1);
for (int j = l; j < h; j++){
if (array[j] <= k){
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[h]);
return (i + 1);
}
void Quick_Sort(int array[], int l, int h) {
if (l < h){
int p = partition(array, l, h);
Quick_Sort(array, l, p - 1);
Quick_Sort(array, p + 1, h);
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
Quick_Sort(arr, 0, n - 1);
printf("Sorted array is : \n");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}