forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.cpp
33 lines (29 loc) Β· 1.11 KB
/
4.cpp
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] << ' ';
}
}