# stepchowfun/kselect.py Last active Feb 1, 2017
 the other algorithm you were mentioning is quick select `````` def quickselect(alist, k): start, end = 0, len(alist) - 1 return quickselecthelper(alist, start, end, k) def quickselecthelper(alist, start, end, k): if start <= end: split = random_partition(alist, start, end) print start + k, split, start, end if k == split: print "found" return alist[start + k] elif k < split: return quickselecthelper(alist, start, split - 1, k) else: return quickselecthelper(alist, split + 1, end, k) def random_partition(alist, start, end): from random import randint pivot = randint(start, end) temp = alist[start] alist[start] = alist[pivot] alist[pivot] = temp leftmark = start + 1 rightmark = end done = False pivotvalue = alist[start] while not done: while leftmark <= rightmark and alist[leftmark] <= pivotvalue: leftmark += 1 while rightmark >= leftmark and alist[rightmark] >= pivotvalue: rightmark -= 1 if leftmark > rightmark: done = True else: swap(alist, leftmark, rightmark) swap(alist, start, rightmark) return rightmark def swap(alist, a, b): temp = alist[a] alist[a] = alist[b] alist[b] = temp def random_partition(alist, start, end): from random import randint pivot = randint(start, end) swap(alist, start, pivot) leftmark = start + 1 rightmark = end done = False pivotvalue = alist[start] while not done: while leftmark <= rightmark and alist[leftmark] <= pivotvalue: leftmark += 1 while rightmark >= leftmark and alist[rightmark] >= pivotvalue: rightmark -= 1 if leftmark > rightmark: done = True else: swap(alist, leftmark, rightmark) swap(alist, start, rightmark) return rightmark ``````