소스코드
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 46 | #include <cstdio> #include <algorithm> using namespace std; const int MX = 50001; int partition( int * a, int left, int right) { int pivot_idx = (left + right) / 2; int pivot_val = a[pivot_idx]; swap(a[right], a[pivot_idx]); int cnt = left; for ( int i = left; i < right; ++i) { if (a[i] <= pivot_val) { swap(a[i], a[cnt++]); } } swap(a[right], a[cnt]); return cnt; } void qsort ( int * a, int l, int r) { if (l <= r) { int pivot = partition(a, l, r); qsort (a, pivot + 1, r); qsort (a, l, pivot - 1); } } int main() { int n, a[MX]; scanf ( "%d" , &n); for ( int i = 0; i < n; ++i) scanf ( "%d" , &a[i]); qsort (a, 0, n - 1); for ( int i = 0; i < n; ++i) { printf ( "%d\n" , a[i]); } return 0; } |
공부한 것을 정리하기 위한 기록입니다.
틀린 부분이 있다면 부드럽게 알려주세요.
'Algorithm' 카테고리의 다른 글
[Algorithm] Heap Sort (0) | 2020.10.26 |
---|---|
[Algorithm] Merge Sort (0) | 2020.10.24 |
[Algorithm] Quick Selection (0) | 2020.10.23 |
[Algorithm] Master Theorem (feat. Quick Selection) (0) | 2020.10.23 |
댓글