QuickSelect

本文介绍了一种高效的查找数组中第k小元素的算法——快速选择算法。该算法基于快速排序的思想,通过一次划分操作将数组分为两部分,使得一部分的所有元素都不大于另一部分的所有元素。通过对目标元素所在部分进行递归操作,可以快速找到目标元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值