-
Notifications
You must be signed in to change notification settings - Fork 2
/
04_quickSort.c
45 lines (41 loc) ยท 1012 Bytes
/
04_quickSort.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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// ํ๊ท ์๋ : O(N*logN)
int number = 10;
int data[10] = { 10,1,5,8,7,6,4,3,2,9 };
void quickSort(int* data, int start, int end) {
if (start >= end) { // ์์๊ฐ 1๊ฐ์ธ ๊ฒฝ์ฐ
return;
}
int key = start; //pivot ๊ฐ
int i = start + 1; //์์ํ๋๊ฐ
int j = end; // ๋ค์์ ์์ํ๋๊ฐ
int temp; // ์์๋ณ์
while(i <= j) { // ์๊ฐ๋ฆด๋๊น์ง ๋ฐ๋ณต (i>j)์ผ๋๊น์ง
while (data[i] <= data[key]) { // ํค ๊ฐ๋ณด๋ค ํฐ๊ฐ์ ๋ง๋ ๋๊น์ง ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋
i++;
}
while (data[j] >= data[key]&&j>start) { // ํค ๊ฐ๋ณด๋ค ์์๊ฐ์ ๋ง๋ ๋๊น์ง ์ผ์ชฝ์ผ๋ก ์ด๋
j--; // start๋ณด๋ค๋ ์ปค์ผํจ
}
if (i > j) { // ํ์ฌ ์๊ฐ๋ฆฐ ์ํ๋ฉด ํค ๊ฐ๊ณผ ๊ต์ฒด
temp = data[j];
data[j] = data[key];
data[key] = temp;
}
else {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
quickSort(data, start, j - 1);
quickSort(data, j+1, end);
}
int main(void) {
quickSort(data, 0, number - 1);
for (int i = 0; i < number; i++) {
printf("%d ", data[i]);
}
return 0;
}