forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.cpp
More file actions
33 lines (29 loc) ยท 1.11 KB
/
4.cpp
File metadata and controls
33 lines (29 loc) ยท 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
int n = 10;
int arr[10] = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};
void quickSort(int* arr, int start, int end) {
if (start >= end) return; // ์์๊ฐ 1๊ฐ์ธ ๊ฒฝ์ฐ ์ข
๋ฃ
int pivot = start; // ํผ๋ฒ์ ์ฒซ ๋ฒ์งธ ์์
int left = start + 1;
int right = end;
while (left <= right) {
// ํผ๋ฒ๋ณด๋ค ํฐ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ๋๊น์ง ๋ฐ๋ณต
while (left <= end && arr[left] <= arr[pivot]) left++;
// ํผ๋ฒ๋ณด๋ค ์์ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ๋๊น์ง ๋ฐ๋ณต
while (right > start && arr[right] >= arr[pivot]) right--;
// ์๊ฐ๋ ธ๋ค๋ฉด ์์ ๋ฐ์ดํฐ์ ํผ๋ฒ์ ๊ต์ฒด
if (left > right) swap(arr[pivot], arr[right]);
// ์๊ฐ๋ฆฌ์ง ์์๋ค๋ฉด ์์ ๋ฐ์ดํฐ์ ํฐ ๋ฐ์ดํฐ๋ฅผ ๊ต์ฒด
else swap(arr[left], arr[right]);
}
// ๋ถํ ์ดํ ์ผ์ชฝ ๋ถ๋ถ๊ณผ ์ค๋ฅธ์ชฝ ๋ถ๋ถ์์ ๊ฐ๊ฐ ์ ๋ ฌ ์ํ
quickSort(arr, start, right - 1);
quickSort(arr, right + 1, end);
}
int main(void) {
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << ' ';
}
}