def selectKth(ar, left, right, k): #1 <= k <= right-left #idx = selectPivotIndex(ar,left,right); if left < right: pivot_index = QuickSort.subroutine(ar, left, right) #pay attention to this condition #the Kth means (k-1)th in a array(given that array start with 0) index = left + (k - 1) if pivot_index == index: return pivot_index if (index < pivot_index): return selectKth(ar, left, pivot_index, k) else: return selectKth(ar, pivot_index + 1, right, (index - pivot_index)) return
def selectKth(ar,left,right,k): #1 <= k <= right-left #idx = selectPivotIndex(ar,left,right); if left<right: pivot_index = QuickSort.subroutine(ar, left, right) #pay attention to this condition #the Kth means (k-1)th in a array(given that array start with 0) index = left+(k-1) if pivot_index == index: return pivot_index if (index < pivot_index): return selectKth(ar,left,pivot_index,k) else: return selectKth(ar,pivot_index+1,right,(index-pivot_index)) return