Skip to content

Commit 741f707

Browse files
committed
adding quick_sort
1 parent ab9514b commit 741f707

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

a.exe

508 Bytes
Binary file not shown.

quick_sort.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void quick_sort(int *arr, int n) {
5+
int temp;
6+
int pivot = arr[n / 2];
7+
int i = 0;
8+
int j = n - 1;
9+
while (i <= j) {
10+
while (arr[i] < pivot) {
11+
i++;
12+
}
13+
while (arr[j] > pivot) {
14+
j--;
15+
}
16+
if (i <= j) {
17+
temp = arr[i];
18+
arr[i] = arr[j];
19+
arr[j] = temp;
20+
i++;
21+
j--;
22+
}
23+
}
24+
if (j > 0) {
25+
quick_sort(arr, j + 1);
26+
}
27+
if (i < n) {
28+
quick_sort(arr + i, n - i);
29+
}
30+
}
31+
32+
int main(int argc, char** argv) {
33+
int arr[] = { 5, 4, 3, 2, 1 };
34+
int n = sizeof(arr) / sizeof(arr[0]);
35+
36+
quick_sort(arr, n);
37+
38+
for (int i = 0; i < n; i++) {
39+
printf("%d ", arr[i]);
40+
}
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)