Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def kth_largest_in_an_array(numbers, k):
- def helper(numbers,start,end,index):
- pivot=random.randint(start,end)
- numbers[start],numbers[pivot]=numbers[pivot],numbers[start]
- left = start
- mid = start
- right = end
- while mid <= right:
- if numbers[mid] < numbers[start]:
- left = left + 1
- numbers[left], numbers[mid] = numbers[mid], numbers[left]
- mid = mid+1
- elif numbers[mid] == numbers[start]:
- mid = mid+1
- else:
- numbers[mid], numbers[right] = numbers[right], numbers[mid]
- right = right-1
- numbers[start], numbers[left] = numbers[left], numbers[start]
- if left <= index and index <= right:
- return
- elif index < left:
- helper(numbers,start,left-1,index)
- elif index > right:
- helper(numbers,right+1,end,index)
- helper(numbers,0,len(numbers)-1,len(numbers)-k)
- result = numbers[len(numbers)-k]
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement