I just needs some guidance please

Back to Problem Solutions forum

Ivan Camilo Gamez     2022-01-06 05:47:15

So long story short, I was excited to see the bubble sort task in the names as bubble sort is one of the functions that costed me the most to learn. But after sometime typing it I got the jist of it.
The issue is that the way I do this sort is not actually counting properlly the number of passes:

def bubblesort(array):
passCounter = 0
swapsCounter = 0
for i in range(0, len(array)):
    passcounter += 1
    for j in range(i, len(array)):
        if array[i] > array[j]:
            temp = array[i]
            array[i] = array[j]
            array[j] = temp
            swapscounter += 1
solution = []
solution.append(passCounter)
solution.append(swapscounter)

return solution

I definetly know the issue is that I am not counting the passes as they expect me to, but then I don't know how should I do it, I need some help please.

gardengnome     2022-01-06 07:40:06
User avatar

A key characteristic of bubble sort is that it always looks at neighbouring values, i.e. pairs of values with indices i and i+1. Your comparison array[i] > array[j] compares more distant values, i.e. the loops are not quite correct for bubble sort.

Rodion (admin)     2022-01-06 08:15:16
User avatar

Yep, probably your code implements Selection Sort instead. Meanwhile your code for "Bubble in Array" does the right thing. It was supposed that code just needs to be slightly extended to become bubble sort...

Please login and solve 5 problems to be able to post at forum