구현 - 병합 정렬, 퀵 정렬, 기수 정렬
2018. 3. 14. 03:40
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include <iostream> #include <ctime> using namespace std; constexpr int arrSize = 200000; void merge(int A[], int left, int mid, int right) { static int sorted[arrSize]; int i = left; //left side int j = mid + 1; //right side int k = left; //sorted index while( i <= mid && j <= right) sorted[k++] = A[i] <= A[j] ? A[i++] : A[j++]; if (i > mid) while (j <= right) sorted[k++] = A[j++]; else while (i <= mid) sorted[k++] = A[i++]; for (int i = left; i <= right; ++i) A[i] = sorted[i]; } void mergeSort(int* a, int left, int right) { if (left < right) { int mid = (left + right) / 2; mergeSort(a, left, mid); mergeSort(a, mid + 1, right); merge(a, left, mid, right); } } void mergeSort(int* a, int size) { cout << "merge sort" << endl; mergeSort(a, 0, size - 1); } int partition(int A[], int left, int right) { int low = left; int high = right + 1; int pivot = A[left]; swap(A[low], A[high]); while (low < high) { swap(A[low], A[high]); while (++low <= right && A[low] < pivot); while (--high >= left && A[high] > pivot); } swap(A[left], A[high]); return high; } void quickSort(int A[], int left, int right) { if (left < right) { int q = partition(A, left, right); quickSort(A, left, q - 1); quickSort(A, q + 1, right); } } void quickSort(int* a, int size) { cout << "quick sort" << endl; quickSort(a, 0, size - 1); } #include <queue> void radixSort(int* a, int size) { cout << "radix sort" << endl; constexpr int ndigits = 2; constexpr int decimal = 10; queue<int> ques[10]; int factor = 1; for(int d = 0; d < ndigits; ++d) { for (int i = 0; i < size; ++i) ques[(a[i] / factor) % decimal].push(a[i]); for (int b = 0, i = 0; b < decimal; ++b) while (!ques[b].empty()) { a[i++] = ques[b].front(); ques[b].pop(); } factor *= 10; } } void testSort(void(*sortFunc)(int*, int)) { int arr[arrSize]; cout << "size of n " << arrSize << endl; for (int i = 0; i < arrSize; ++i) arr[i] = rand() % 99; clock_t start, finish; double duration; start = clock(); sortFunc(arr, arrSize); finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; cout << "duration : " << duration << endl; if(arrSize < 100) for (int& a : arr) cout << a << " "; cout << endl << endl; } int main() { srand(size_t(time(nullptr))); testSort(mergeSort); testSort(quickSort); testSort(radixSort); return 0; } | cs |
밑과 같이 구현한 기수 정렬이 생각보다 느린데 왜 그런 것일까?
1. 나눗셈?
2. deque기반 queue 문제?
'프로그래밍' 카테고리의 다른 글
간단한 큐 테스트 (0) | 2018.03.31 |
---|---|
03/26 자료구조들 (0) | 2018.03.26 |
구현 - 버블정렬, 선택 정렬, 삽입 정렬, 셸 정렬 (0) | 2018.03.14 |
구현 - 다익스트라 dijkstra, 플로이드 floyd (0) | 2018.03.12 |
구현 - 프림 알고리즘 (0) | 2018.03.12 |