def quicksort(L, low, high):
total = 0
if low < high:
pivot_location, total = Partition(L, low, high)
total += quicksort(L,low,pivot_location)
total += quicksort(L,pivot_location + 1, high)
return total
def Partition(L, low, high):
print(L)
#print(high)
total = 0
if (high - low) % 2 == 0:
middle = (high - low) // 2 - 1
else:
middle = ((high - low) // 2) + low
print(L[low])
print(L[high - 1])
print(L[middle])
numbers = []
numbers.append(L[low])
numbers.append(L[middle])
numbers.append(L[high - 1])
if L[low] > L[middle] and L[low] < L[high - 1]:
pivot = L[low]
elif L[low] >L[high -1] and L[low] < L[middle]:
pivot = L[low]
elif L[middle] < L[low] and L[middle] > L[high -1]:
temp = L[low]
L[low] = L[middle]
L[middle] = temp
pivot = L[low]
elif L[middle] < L[high - 1] and L[middle] > L[low]:
temp = L[low]
L[low] = L[middle]
L[middle] = temp
pivot = L[low]
elif L[high - 1] < L[low] and L[high - 1] > L[middle]:
temp = L[low]
L[low] = L[high - 1]
L[high - 1] = temp
pivot = L[low]
else:
temp = L[low]
L[low] = L[high - 1]
L[high - 1] = temp
pivot = L[low]
print(pivot)
i = low + 1
for j in range(low + 1,high,1):
total += 1
if L[j] < pivot:
temp = L[j]
L[j] = L[i]
L[i] = temp
i += 1
temp = L[low]
L[low] = L[i -1]
L[i - 1] = temp
#print(L)
return i - 1,total
c = [2148,9058,7742,3153,6324,609,7628,5469,7017,504]
print(quicksort(c, 0, len(c)))
print(c)
I have implemented quicksort this way. I tried using first element as pivot and last element as pivot and it worked perfectly. However I can't figure out how to choose as a pivot the median of the three elements: first, middle, last. Any help on how to choose in each recursive call the middle element. Note that I want to pass as an argument to each recursive call the whole array because I don't want to use any extra memory.
middle = (high - low) // 2 - 1
should probably have had ahigh+low
and no-1
. In any case:middle = (low + high) // 2
should be good for both odd and evenhigh-low
- Dan Getz