forked from seeditsolution/pythonprogram
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSelect.py
More file actions
Latest commit
29 lines (27 loc) · 975 Bytes
/
Copy pathQuickSelect.py
File metadata and controls
29 lines (27 loc) · 975 Bytes
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
defquickselect(array,startind,endind,position):
whileTrue:
pivotind=startind
leftmark=startind+1
rightmark=endind
whileleftmark<=rightmark:
ifarray[leftmark] >array[pivotind] andarray[rightmark] <array[pivotind]:
swap(leftmark,rightmark,array)
ifarray[leftmark] <=array[pivotind]:
leftmark+=1
ifarray[rightmark] >=array[pivotind]:
rightmark-=1
swap(pivotind, rightmark,array)
ifrightmark==position:
print(array[rightmark])
return
elifrightmark<position :
startind=rightmark+1
else :
endind=rightmark-1
defswap(one,two,array):
array[one],array[two]=array[two],array[one]
for_inrange(int(input())):
n=int(input())
arr=list(map(int,input().split()))
k=int(input())
quickselect(arr,0,n-1,k-1)