Eurovision Results Sort Project

Eurovision Results Sort Project

Python Sort Project

Given the last year results of Eurovision, the annual international song competition featuring countries from Europe:

  • Albania: 76
  • Armenia:122
  • Austria:120
  • Belgium:182
  • Croatia:123
  • Finland:526
  • France:104
  • Germany:18
  • Italy:350
  • Poland:92
  • Portugal:59
  • UK:24

Write a Python function to sort the countries in descending order by their number of points so you can know what country that won the contest. Use the bubble sort algorithm.

The bubble sort algorithm in Python is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It passes through the list multiple times until the list is sorted. On each pass, the largest element bubbles up to its correct position, which is why it’s called “bubble” sort.

This Python sort project will help you understand how to sort Python dictionaries and will boost your problem solving skills. Click here for more Python projects.

Source Code

## Write the function to implement the algorithm
def bubble_sort_dict(d):
    # Convert the dictionary to a list of tuples for sorting
    items = list(d.items())
    n = len(items)
    
    # Apply the bubble sort algorithm
    for i in range(n-1):
        for j in range(0, n-i-1):
            if items[j][1] < items[j+1][1]:
                items[j], items[j+1] = items[j+1], items[j]
    
    # Convert the sorted list of tuples back to a dictionary
    sorted_dict = dict(items)
    
    return sorted_dict

Expected Output:


Eurovision = {"Austria": 120, "Portugal": 59, "France": 104, "Spain": 100, 
"Albania": 76, "Finland": 526, "Belgium": 182, "Germany": 18, "UK": 24,
 "Poland": 92, "Italy": 350, "Armenia": 122, "Norway": 268, "Croatia": 123}

sorted_eurovision = bubble_sort_dict(Eurovision)
print(sorted_eurovision)

## Output
{'Finland': 526, 'Italy': 350, 'Norway': 268, 'Belgium': 182, 'Croatia': 123, 'Armenia': 122, 'Austria': 120, 'France': 104, 'Spain': 100, 'Poland': 92, 'Albania': 76, 'Portugal': 59, 'UK': 24, 'Germany': 18}

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart