Bubble Sort(Descending) of Tuple in Python
Write a program that sorts a list of tuple-elements in descending order of Points using Bubble sort. The tuple-elements of the list contain following information about different players: (PlayerNo, Playername, Points) Sample content of the list before sorting: [(103, Ritika, 3001), (104, John, 2819), (101, Razia, 3451), (105, Tarandeep, 2971) ] After sorting the list would be like: [ (101, Razia, 3451), (103, Ritika, 3001), (105, Tarandeep, 2971), (104, John, 2819) ]
PYTHON PROGRAMMING
# Sample list of tuple-elements
players = [(103, "Ritika", 3001), (104, "John", 2819), (101, "Razia", 3451), (105, "Tarandeep", 2971)]
# Bubble sort algorithm to sort the list in descending order of Points
n = len(players)
for i in range(n):
for j in range(0, n-i-1):
# Compare the points of adjacent tuple-elements
if players[j][2] < players[j+1][2]:
# Swap if the points of the current tuple-element are less than the next one
players[j], players[j+1] = players[j+1], players[j]
# Print the sorted list
print("Sorted list of players by points (descending order):")
for player in players:
print(player)