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 | int partitionRandom(int* arr, int start, int end) { int temp; do { temp = rand()%end; } while (temp < start); int pivot = arr[temp]; int left = start; int right = end; while (left <= right) { while (arr[left] < pivot && left < end) left++; while (arr[right] > pivot && right > start) right--; if (right < left) Swap(&arr[left], &arr[right]); } Swap(&arr[right], &arr[start]); return right; } void SelectionSearch(int* arr, int start, int end, int pos) { int pivot = partitionRandom(arr, start, end); int S = pivot - 1 - start + 1; if ( pos == S + 1) return; if ( pos < S + 1) return SelectionSearch(arr, start, pivot-1, pos); else return SelectionSearch(arr, pivot + 1, end, pos - S - 1 ); } | cs |
'알고리즘' 카테고리의 다른 글
| BinarySearch (0) | 2018.05.26 |
|---|---|
| Find Min, Find Second (0) | 2018.05.26 |
| InsertSort (0) | 2018.05.26 |
| SelectionSort (0) | 2018.05.26 |
| BubbleSort (0) | 2018.05.26 |