Quick Sort
Problem statement
Sample Input0: [1, 2, 3, 4]
Sample Output0: [1, 2, 3, 4]
Sample Input1: [5, 12, 3, 21, 4]
Sample Output1: [3, 4, 5, 12, 21]
Sample Input2: [7.1, 9.2, 3.1]
Sample Output2: [3.1, 7.1, 9.2]Solution key
def partition(lst, start, end, pivot):
lst[pivot], lst[end] = lst[end], lst[pivot]
store_index = start
for i in range(start, end):
if lst[i] < lst[end]:
lst[i], lst[store_index] = lst[store_index], lst[i]
store_index += 1
lst[store_index], lst[end] = lst[end], lst[store_index]
return store_index
def quick_sort(lst, start, end):
if start >= end:
return lst
pivot = round((start+end)/2)
new_pivot = partition(lst, start, end, pivot)
quick_sort(lst, start, new_pivot - 1)
quick_sort(lst, new_pivot + 1, end)
def sort(lst):
if (len(lst)==0):
return "invalid input"
for i in lst:
result=isinstance(i,str)
if result==True:
return "invalid input"
break
quick_sort(lst, 0, len(lst) - 1)
#print(lst)
return lstCloudCoder exercises
Pre-Lab Questions
Post-Lab Questions
Bonus
Interview Grade
Related Material
The Quicksort Dance

Compare Merge and Quick
Last updated