6

I just needs some guidance please

 2 years ago
source link: https://www.codeabbey.com/index/forum_topic/c771b6a99544f1d04ef68279b8970ceb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK