-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.py
29 lines (27 loc) · 812 Bytes
/
5.py
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
class Solution:
# @param k & A a integer and an array
# @return ans a integer
def quicksort(self, arr, head, tail, k):
i = head + 1
j = tail
while i != j:
while i != j and arr[j] < arr[head]:
j -= 1
while i != j and arr[i] > arr[head]:
i += 1
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
if arr[head] < arr[i]:
temp = arr[head]
arr[head] = arr[i]
arr[i] = temp
else :
i = head
if i - 1 > head: Solution.quicksort(arr, arr, head, i - 1)
if tail > i + 1: Solution.quicksort(arr, arr, i + 1, tail)
return arr
def kthLargestElement(self, k, A):
return Solution.quicksort(A, A, 0, len(A) - 1,k)
B = [9,3,2,4,8]
print(Solution.kthLargestElement(B, 3, B))