-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-sort.c
40 lines (36 loc) · 884 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
#include <stdio.h>
void swap (int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition (int arr[], int l, int h) {
int pivot = arr[l];
int i = l, j = h;
while (i < j) {
while (arr[i] <= pivot && i < h) i ++;
while (arr[j] > pivot && j > l) j --;
if (i < j) swap(&arr[i], &arr[j]);
}
swap(&arr[l], &arr[j]);
return j;
}
void quicksort (int arr[], int l, int h) {
if (l < h) {
int part = partition(arr, l, h);
quicksort(arr, l, part - 1);
quicksort(arr, part + 1, h);
}
}
int main () {
int n;
printf("N = ");
scanf("%d", &n);
int arr[n];
printf("Enter elements : ");
for (int i = 0; i < n; i ++) scanf("%d", &arr[i]);
quicksort(arr, 0, n - 1);
printf("\nSorted array : ");
for (int i = 0; i < n; i ++) printf("%d ", arr[i]);
return 0;
}