Here is how to sort using Quick Sort in Python.
Run this code here: https://repl.it/@VinitKhandelwal/quick-sort
Run this code here: https://repl.it/@VinitKhandelwal/quick-sort
def quickSort(list1, left, right):
length = len(list1)
if left < right:
pivot = right
partitionIndex = partition(list1, pivot, left, right)
quickSort(list1, left, partitionIndex - 1)
quickSort(list1, partitionIndex + 1, right)
return list1
def partition(list1, pivot, left, right):
pivotValue = list1[pivot]
partitionIndex = left
for i in range(left, right):
if list1[i] < pivotValue:
swap(list1, i, partitionIndex)
partitionIndex += 1
swap(list1, right, partitionIndex)
return partitionIndex
def swap(list1, firstIndex, secondIndex):
temp = list1[firstIndex]
list1[firstIndex] = list1[secondIndex]
list1[secondIndex] = temp
numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]
print(quickSort(numbers, 0, len(numbers)-1))
Comments
Post a Comment