桃花长相依
对此问题的最佳解决方案如下:使用快速排序找到轴,并丢弃该第k个元素所在的部分,然后递归地找到下一个轴。(这是kth Max finder,您需要更改if else条件使其成为kth Min Finder)。这是JavaScript代码- // Complexity is O(n log(n)) var source = [9, 2, 7, 11, 1, 3, 14, 22]; var kthMax = function(minInd, MaxInd, kth) { // pivotInd stores the pivot position // for current iteration var temp, pivotInd = minInd; if (minInd >= MaxInd) { return source[pivotInd]; } for (var i = minInd; i < MaxInd; i++) { //If an element is greater than chosen pivot (i.e. last element) //Swap it with pivotPointer element. then increase ponter if (source[i] > source[MaxInd]) { temp = source[i]; source[i] = source[pivotInd]; source[pivotInd] = temp; pivotInd++; } } // we have found position for pivot elem. // swap it to that position place . temp = source[pivotInd]; source[pivotInd] = source[MaxInd]; source[MaxInd] = temp; // Only try to sort the part in which kth index lies. if (kth > pivotInd) { return kthMax(pivotInd + 1, MaxInd, kth); } else if (kth < pivotInd) { return kthMax(minInd, pivotInd - 1, kth); } else { return source[pivotInd]; } } // last argument is kth-1 , so if give 2 it will give you, // 3rd max which is 11 console.log(kthMax(0, source.length - 1, 2));