![]() |
||||||
|
|
Finding the k-th smallest integer - The most obvious one is by sorting the array and picking the k-th element of the array. But this method does more than what it is requested. There is another method, which will be described here, that picks the k-th smallest integer. Problem Description We have an arbitrary array of integer [first..last]. We have to choose the k-th smallest integer. Algorithm 1. Choose a pivot from the array 2. Partition the array so that: A[first...pivotIndex-1] <= pivot <= A[pivotIndex+1..last] 3. if k < pivotIndex then it must be on the left of pivot, so do the same method recursively on the left part 4. if k = pivotIndex then it must be the pivot 5. if k > pivotIndex then it must be on the right of pivot, so do the same method recursively on the right part Implementation in C++
|
|||||
| Copyright ©
2004 - Harvest Software |
||||||