I have been working on a quick sort algorithm which specifically sets a wall to the left, current element just to the right of the wall and the pivot at the end of the data set. The sort will compare the current element with the pivot and increment the current element each time it is greater than the pivot. When it is smaller or equal to the pivot it should swap the current element with the item to the right of the wall and move the wall one place to the right.
Here is my code:
def QuickSort(dataset, low, high):
if (low >= high):
return dataset
else:
#Set pivot to last element
pivot = high
#Set wall at first element
wall = low
#Loop through list - if current element is less or equal
#to the pivot, swap current element with element right of wall
#and move wall on.
for i in range(high):
if (dataset[i] <= pivot):
temp = dataset[wall]
dataset[wall] = dataset[i]
dataset[i] = temp
wall = wall + 1
if wall >= high:
break
#recursively call for subsequent part of list
QuickSort(dataset, low, wall-1)
QuickSort(dataset, wall, high)
dataset = [1,6,2,3,6,7,4,2,5]
dataset_length = len(dataset)
sorted_data = QuickSort(dataset, 0, dataset_length)
print(sorted_data)
input()
When running the code, the dataset is not returned and I can't quite see where I have gone wrong. Thank you for your help.
elseblock executes, your function will not hit a return statement, so it will returnNone. Possibly you mean to havereturn datasetat the end.