-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkth_largest.py
More file actions
77 lines (62 loc) · 2.15 KB
/
kth_largest.py
File metadata and controls
77 lines (62 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Govinda KC
# CS2302 10:30
# Lab 5A
# Professor Aguirre, Diego
# TA Saha Manoj
# Date of last modification: 11/27/2018
# Extra credit problemt
class Solution:
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
# Sort the list
self.quicksort(nums, 0, len(nums) - 1)
# Return the k largest element
return nums[len(nums) - k]
def quicksort(self, nums_list, start, end):
# If end crosses or is equal to start, list is sorted
if end <= start:
return
# Split the list into low and high partitions
index_high = self.split_list(nums_list, start, end)
# Recursively sort the two resulting lists
self.quicksort(nums_list, start, index_high)
self.quicksort(nums_list, index_high + 1, end)
def split_list(self, nums_list, start, end):
low = start
high = end
# Get midpoint and pivot
mid = (high - low) // 2 + low
pivot = nums_list[mid]
while True:
# While element in list is less than pivot,
# move right in list
while nums_list[low] < pivot:
low += 1
# While element in list is greater than pivot,
# move left in the list
while nums_list[high] > pivot:
high -= 1
# If low is greater than or equal to high,
# splitting is finished
if low >= high:
break
# Otherwise, swap the two elements where we stopped
# and keep going
temp = nums_list[low]
nums_list[low] = nums_list[high]
nums_list[high] = temp
# Move high and low pointers
low += 1
high -= 1
# Returns the last index of the low partition(where high stopped)
return high
# Used to test the functions
cl = Solution()
test_list = [4,7,8,9,2,14,18,23,5,34,77]
k = 8
kth_largest = cl.findKthLargest(test_list, k)
print("The", k, "largest number is",kth_largest)