def quicksort(nums):
l = 0
r = len(nums)-1
_quicksort(nums, l, r)
def _quicksort(nums, left, right):
l = left
r = right
if l < r:
tmp = nums[l]
while l < r:
while l < r and tmp <= nums[r]:
r -= 1
nums[l], nums[r] = nums[r], nums[l]
while l < r and tmp > nums[l]:
l += 1
nums[l], nums[r] = nums[r], nums[l]
_quicksort(nums, left, l)
_quicksort(nums, l+1, right)
nums = [6,2,4,1,2,3,5,2,7]
quicksort(nums)
print(nums)
def findKthLargest(numbers, start, end, k):
if k < 0 or numbers == [] or start < 0 or end >= len(numbers) or k > end:
return None
low = start
high = end
key = numbers[start]
while low < high:
while low < high and numbers[high] >= key:
high -= 1
numbers[low] = numbers[high]
while low < high and numbers[low] <= key:
low += 1
numbers[high] = numbers[low]
numbers[low] = key
if low < k:
return partitionOfK(numbers, low+ 1, end, k)
elif low > k:
return partitionOfK(numbers, start, low- 1, k)
else:
return numbers[low]
numbers = [3,5,6,7,2,-1,9,3]
print(sorted(numbers))
print(partitionOfK(numbers, 0, len(numbers) - 1, 5))