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 | //q_sort #include <iostream> void qsort(int num[], int left, int right) { int pivot = num[(left+right) / 2]; //중앙 값 int l = left; int r = right; while (l < r) { while (pivot > num[l]) l++; while (pivot < num[r]) r--; if (l <= r) { int ch = num[l]; num[l] = num[r]; num[r] = ch; l++; r--; } } if (left < r) qsort(num, left, r); if (right > l) qsort(num, l, right); } int main(void) { int n[8] = { 67, 90, 57, 25, 84, 32, 73, 54 }; qsort(n, 0, 7); for (int i = 0; i < 8; i++) { std::cout << n[i] << " "; } std::cout << std::endl; return -1; } |
'개발 > 자료구조' 카테고리의 다른 글
[자료구조] c++ 스택 (0) | 2016.11.17 |
---|---|
[자료구조] [C++] CircularQueue 사이클 큐 (0) | 2016.11.16 |
[자료구조] Select Sort (0) | 2016.10.26 |
Recursive 를 이용한 Permutation Generator (순열 생성기) (0) | 2016.10.25 |
Binary Search 이진 탐색 (0) | 2016.10.25 |