Back to Problem Solutions forum
TL;DR In place sort works 100% in python3 but I cannot get my left-right answers to match the solution provided
Hey there,
Eventually figured out the quicksort 122 in python3 with some help from the psuedocode on RosetteCode.org (seemed more logical to me than the wikipedia page) Managed to get it working (with some hiccups here and there and not too much cribbing) and understand the logic of the sort etc but although my sort works (i.e. array is sorted correctly), my left-right values dont match the codeabbey solution. They start out the same, but then deviate after.
Maybe its a simple as the position of my print statement or is it more complicated? Rhe reason for this mismatch is not obvious to me. Can anyone help?
ps. I am self-taught and while I can fudge some code together, I would never consider myself a programmer and I do this for fun (learning python for some iot & microcontroller crap) so I have zero classical programming education...
My code:
number_of_values_in_array = int(input())
array_of_values = list(map(int, input().split()))
def quicksort(array, first, last):
if last - first > 0:
pivot = array[first]
left = first
right = last
print(("{}-{}").format(left, right), end=" ")
while left <= right:
while array[left] < pivot:
left = left + 1
while array[right] > pivot:
right = right - 1
if left <= right:
array[left], array[right] = array[right], array[left] #must be one line
left = left + 1
right = right - 1
quicksort(array, first, right)
quicksort(array, left, last)
return
quicksort(array_of_values, 0, len(array_of_values) - 1)
10 38 23 9 19 113 5 42 85 71 112
gives in my code: 0-9 0-3 1-3 1-2 4-9 4-8 4-7 4-5 6-7
codeabbey expects 0-9 0-3 1-3 1-2 5-9 5-8 5-7