减治法——搜索第k小元素(Decrease and Conquer by a Factor - Finding the kth smallest element)
2017-06-11 16:32
357 查看
减治法——搜索第k小元素(Decrease and Conquer by a Factor - Finding the kth smallest element)
问题(Problem)Find the kth smallest element in an unsorted array.
分割(Partitioning)
Partitioning an array around some pivot element p means reorganising the array so that all elements to the left of p are no greater than p, while those to the right are now smaller.
Lomuto Partitioning
function LomutoPartition(A[lo..hi]) p ⟵ A[lo] s ⟵ lo for i ⟵ lo+1 to hi do if A[i] < p then s ⟵ s + 1 swap(A[s], A[i]) swap(A[lo], A[s]) return s
算法代码(Algorithm Pseudocode)
function QuickSelect(A[lo, hi], k) s ⟵ LomutoPartition(A[lo..hi]) if s = lo + k - 1 then return A[s] if s < lo + k -1 then QuickSelect(A[s+1..hi], k-s-1) else QuickSelect(A[lo..s-1], k)
Java code
I will update ASAP.
最后再说两句(PS)
Welcome questions always and forever.
相关文章推荐
- 减治法——内插法搜索(Decrease and Conquer by a Factor - Interpolation Search)
- 有序矩阵中查找第k小的元素 Kth smallest element in a row-wise and column-wise sorted 2D array
- 减治法——二分查找算法(Decrease and Conquer by a Factor - Binary Search Algorithm)
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
- [C++]Kth Smallest Element in a BST 在一个二叉排序树中找第k小的元素
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
- The kth largest element in max-heap 最大堆的第k大元素
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
- LeetCode OJ 之 Kth Smallest Element in a BST(BST中的第k小元素)
- Kth Smallest Element in a BST 寻找二叉排序树中第k小元素
- Swap kth element from the beginning and kth element from the end of linked list -- Amazon
- leetcode 378. Kth Smallest Element in a Sorted Matrix有序矩阵寻找第K小数+二分查找
- 378. Kth Smallest Element in a Sorted Matrix(查找矩阵中第k小的数)
- 数据结构与算法分析:第一章:Finding the kth largest number and The four basic rules of recursion
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
- [leetcode 230]Kth Smallest Element in a BST----求二叉搜索树的第K小值
- give two sorted array, find the k-th smallest element of union of A and B
- Divide and Conquer-215-Kth Largest Element in an Array
- Find the Kth smallest element
- LeetCode OJ 之 Kth Largest Element in an Array(数组中的第k大元素)