function quickfindFirstK(list, left, right, k)
if right > left
select pivotIndex between left and right
pivotNewIndex := partition(list, left, right, pivotIndex)
if pivotNewIndex > k // new condition
quickfindFirstK(list, left, pivotNewIndex-1, k)
if pivotNewIndex < k
quickfindFirstK(list, pivotNewIndex+1, right, k)
function partition(list, left, right, pivotIndex)
pivotValue := list[pivotIndex]
swap list[pivotIndex] and list[right] // Move pivot to end
storeIndex := left
for i from left to right
if list[i] < pivotValue
swap list[storeIndex] and list[i]
increment storeIndex
swap list[right] and list[storeIndex] // Move pivot to its final place
return storeIndex
Top K algorithm
最新推荐文章于 2022-04-15 15:45:23 发布
本文介绍了一种使用快速排序思想查找未排序数组中第K小元素的算法。该算法首先选择一个基准值并进行分区操作,然后递归地在分区后的子数组中继续查找,直至找到目标元素。
1131

被折叠的 条评论
为什么被折叠?



