Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bubblesort
#1
I would like to sort several points from smallest to biggest however. I will wish to get this result:
[Image: 64263364a.png]

However, my ranking seems to be reversed for now :-(
[Image: 53283842b.png]

I think my problem is my function Bubblesort ?

def Bubblesort(name, goal1, point):
    swap = True
    while swap:
        swap = False
        for i in range(len(name)-1):
            if point[i+1] > point[i]:
                goal1[i], goal1[i+1] = goal1[i+1], goal1[i]
                name[i], name[i+1] = name[i+1], name[i]
                point[i], point[i + 1] = point[i + 1], point[i]
                swap = True
    return name, goal1,  point

def ranking(name, point):
  for i in range(len(name)):
    print(name[i], "\t" , point[i], " \t ")
  
name = ["Henry", "Owen", "Drogba"]
point = [0]*3
goal1 = [68, 52, 46]
gain = [6,4,2]

    
name, goal1,  point = Bubblesort( name, goal1,  point  )
for i in range(len(name)):
    point[i] += gain[i]
 
ranking (name, point)
Reply
#2
Please don't paste pictures, use text with output tags.

Your bubblesort function is sorting by points (line 6), which is all zeros. So the if condition is never True, and nothing ever gets moved.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
For reference, here's the original thread: https://python-forum.io/Thread-How-add-2-arrays
I'm not merging, since this is explicitly about sorting, while the other was more... general.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020