I have two days trying to understand why my quicksort function returns all but two items, sorted in the correct order.
When entering
quicksort([0,7,3,11,23,87,999,1023,12,713,5,6,9])
Outputs
[0, 6, 3, 5, 7, 9, 11, 12, 23, 87, 713, 999, 1023]
What do you think is wrong with the function?
def quicksort(array):
if len(array) <= 1:
return array
Pivot = partition(array,0,len(array));
left = array[:Pivot]
right = array[Pivot+1:]
left = quicksort(left)
right = quicksort(right)
left.append(array[Pivot])
return left+right
For completion questions, I add a section function, but I'm pretty sure that the problem does not exist.
def partition(array,left,right):
Pivot = array[left]
i = left+1
for j in range(left+1,right):
if array[j] < Pivot:
array[j], array[i] = array[i], array[j]
i += 1;
array[left], array[i-1] = array[i-1], array[left]
return left