Home > Software design >  Using Bubble Sort, how can I use the second element so I can print in descending order
Using Bubble Sort, how can I use the second element so I can print in descending order

Time:10-08

I was trying to use bubble sort in sorting a linked list in descending order. My code prints out a list in alphabetical order instead. I wanted to print out the second element and sort it from highest to lowest value instead of alphabetical order.

What am I missing here? If someone can point this out, I would entirely be grateful thanks.

class LinkedList:
    def __init__(self, data):
        self.label = data[0][0]
        self.value = data[0][1]
        self.tail = None if (len(data) == 1) else LinkedList(data[1:])

countries = [("Ukraine",41879904),("Brunei",442400),("Christmas Island (Australia)",1928),("Mauritius",1265985),("Lesotho",2007201),("Guatemala",16604026),("British Virgin Islands (UK)",30030),("Malta",493559),("Greenland (Denmark)",56081),("Guernsey (UK)",62792)]


# BUBBLE SORT 
def bubbleSort(countries):
    for passnum in range(len(countries)-1,0,-1):
        for i in range (passnum):
            for j in range(0, len(countries)-i,-1)
                if (countries[j][1] > countries[j 1][1]):
                    temp = countries[j]
                    countries[j] = countries[j 1]
                    countries[j 1] = temp
        return countries

print("Bubble Sort")
print("The Unsorted List: ", countries)

print('\n' * 1)

bubbleSort(countries)
print("The Original Sorted List: ", countries)

print('\n' * 1)

countries.reverse()
print("The Updated List in Descending Order: ", countries)

CodePudding user response:

I'm not sure why you require three nested loops or if the passnum variable has any relevance. I haven't included that in this answer but if it's important the change wouldn't make any difference.

def bubbleSort(countries):
    for i in range(len(countries)):
        for j in range(0, len(countries)-i-1):
            if countries[j][1] > countries[j 1][1]:
                temp = countries[j]
                countries[j] = countries[j 1]
                countries[j 1] = temp
    return countries

Output

Bubble Sort
The Unsorted List:  [('Ukraine', 41879904), ('Brunei', 442400), ('Christmas Island (Australia)', 1928), ('Mauritius', 1265985), ('Lesotho', 2007201), ('Guatemala', 16604026), ('British Virgin Islands (UK)', 30030), ('Malta', 493559), ('Greenland (Denmark)', 56081), ('Guernsey (UK)', 62792)] 


The Original Sorted List:  [('Christmas Island (Australia)', 1928), ('British Virgin Islands (UK)', 30030), ('Greenland (Denmark)', 56081), ('Guernsey (UK)', 62792), ('Brunei', 442400), ('Malta', 493559), ('Mauritius', 1265985), ('Lesotho', 2007201), ('Guatemala', 16604026), ('Ukraine', 41879904)]


The Updated List in Descending Order:  [('Ukraine', 41879904), ('Guatemala', 16604026), ('Lesotho', 2007201), ('Mauritius', 1265985), ('Malta', 493559), ('Brunei', 442400), ('Guernsey (UK)', 62792), ('Greenland (Denmark)', 56081), ('British Virgin Islands (UK)', 30030), ('Christmas Island (Australia)', 1928)]

CodePudding user response:

Python has a built in sorting function, that will probably be faster than a self coded version of Bubble Sort.

This code:

temp = [("ukraine", 852), ("America", 965), ("China", 785)]

nums = [i[1] for i in temp]
nums.sort(reverse=True)
ordered = []
for num in nums:
    for item in temp:
        if num == item[1]:
            ordered.append(item)

returns a sorted list, in descending order. Is using bubble sort required, or did you just pick it as a sorting algorithm?

  • Related