QuickSelect

来源:互联网 发布:网络捕鱼游戏怎么开挂 编辑:程序博客网 时间:2024/05/18 00:44
Algorithm

The idea of the quick select is quite simple: just like with quicksort, select a random element from the list, and place every item that is smaller to the first half of the array, and every element that is equal to or greater than the pivot, in the second half (the ‘half’ is not entirely correct, as it is possible that the result will not be exactly ‘half’).

So a step would look like this:

Arr = [5 1 4 3 2]Pivot = [4]Steps:swap [5] and [2] as 5>=4 and 2<[2 1 4 3 5]swap [4] and [3] as 4>=4 and 3<4[2 1 3 4 5]
When we finish with the first iteration, we know the followings:
All elements <4 are on the left of 4
All elements >=4 are on the right of 4 (including the 4 itself)

So, if we are looking for the first 3 elements, we can stop, we found them. If we are looking for the 3rd element, we need more iteration, but we know we must look for it in the first half of the array hence we can ignore the rest:

Arr = [2 1 3 …]Pivot = [1]Steps:swap [2] and [1] as 2>=2 and 1<2[1 2 3 …]
When we finish this iteration, we know the followings:
All elements <1 are on the left of 1 (none in this case)
All elements >=1 are on the right of 1 (including the 1 itself)

If we were looking for the 1st element, we are done, [1] is the first. However, we know the 3rd element must be right from the [1] and left from [4]:

Arr = […2 3…]Pivot= [2]…
Just like with binary search, we keep dropping a segment from the array as we are getting closer to the solution. On average, we halve the search space so it gives us a geometrical series of operations. In the first step, we work with all the items, which is N. The next iteration works only with roughly the half of the array, which is N/2 and so on:


另一个比较不错的算法解释: http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/05DemoQuickSelect.pdf


package quickselect;public class QuickSelect {public static int quickSelect(int[] arr, int start, int end, int k) {int pivot = partition(arr, start, end);if (pivot == k) {return arr[pivot];} else if (pivot < k) {return quickSelect(arr, pivot + 1, end, k);} else {return quickSelect(arr, start, pivot - 1, k);}}private static int partition(int[] arr, int start, int end) {int pivotIndex = medianOfThreePivotIndex(arr, start, end);int pivot = arr[pivotIndex];swap(arr, pivotIndex, end);int i = start;int j = end - 1;while (i < j) {while (arr[i] <= pivot) i++;while (arr[j] >= pivot) j--;if (i >= j) break;swap(arr, i, j);}swap(arr, i, end);return i;}private static int medianOfThreePivotIndex(int[] arr, int start, int end) {int mid = start + (end - start) / 2;if (arr[mid] < arr[start]) {swap(arr, start, mid);}if (arr[end] < arr[start]) {swap(arr, start, end);}if (arr[end] < arr[mid]) {swap(arr, mid, end);}return mid;}private static void swap(int[] arr, int i, int j) {int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}public static void main(String[] args) {int[] arr = {4,2,1,7,10,5,3,2};System.out.println(quickSelect(arr, 0, 7, 2));}}


0 0