Here is the pseudocode I have
Procedure
i <- n
last <- 1
while i > last do
for j <- 1 to i-1 do
if t[j] > t[j+1] do
t[j] <-> t[j+1] {switch values}
last <- j
end if
end for
i <- last
last <- 1
end while
end
I just need to state why this is an improvement on the standard bubble sort and do a trace of the algorithm. That I can manage. To do some test and for learning purposes, I decided to code it in python. My implementation do not work.
def bubbleSort(arr):
n = len(arr)
i = n
last = 1
while (i > last):
for j in range(0, i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
last = j
i = last
last = 1
# Driver code to test above
arr = [5, 3, 89, 100, -4, 7, 45]
bubbleSort(arr)
The output I get is 3 ,5 ,89,-4 ,7 ,45 ,100 witch mean that it only does the first pass in the inner loop but stop right after.
Is there something I am translating wrong from the pseudocode ?