Created
October 6, 2016 23:53
-
-
Save rasoolims/d309a76af79636ce9256756690e068e1 to your computer and use it in GitHub Desktop.
kth_item.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, random | |
def partition(myList, start, end): | |
pivot = myList[start] | |
left = start+1 | |
right = end | |
done = False | |
while not done: | |
while left <= right and myList[left] <= pivot: | |
left = left + 1 | |
while myList[right] >= pivot and right >=left: | |
right = right -1 | |
if right < left: | |
done= True | |
else: | |
temp=myList[left] | |
myList[left]=myList[right] | |
myList[right]=temp | |
temp=myList[start] | |
myList[start]=myList[right] | |
myList[right]=temp | |
return right | |
arr = list() | |
size = 10 | |
for i in range(0, size): | |
arr.append(random.randint(0,10000)) | |
print arr | |
k = 4 | |
s = 0 | |
e = len(arr)-1 | |
while True: | |
i = partition(arr, s, e) | |
if i==k: | |
print arr | |
print arr[i] | |
break | |
elif i<k: | |
s = i+1 | |
else: | |
e = i-1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment