Insertion Sort(Ascending) of List in Python
Given following list L that stores the names of Nobel prize winners. Each element of the list is a tuple containing details of one Nobel prize winner. L= [("Wilhelm Conrad Rontgen", "Physics", 1901), ("Ronald Ross", "Medicine", 1982), ("Marie Curie", "Physics", 1903) ("Ivan Pavlov", "Medicine", 1904), ("Henryk Sienkiewicz", "Literature", 1905), ("Theodore Roosevelt", "Peace", 1986)] Write a program to sort the list in the order of Last names of the winners. Use insertion sort algorithm.
PYTHON PROGRAMMING
# Given list of Nobel prize winners
L = [("Wilhelm Conrad Rontgen", "Physics", 1901),
("Ronald Ross", "Medicine", 1982),
("Marie Curie", "Physics", 1903),
("Ivan Pavlov", "Medicine", 1904),
("Henryk Sienkiewicz", "Literature", 1905),
("Theodore Roosevelt", "Peace", 1986)]
# Insertion sort algorithm to sort the list based on last names of the winners
for i in range(1, len(L)):
key = L[i]
j = i - 1
# Extract the last name from the tuple and compare
while j >= 0 and L[j][0].split()[-1] > key[0].split()[-1]:
L[j + 1] = L[j]
j -= 1
L[j + 1] = key
# Print the sorted list
print("Sorted list of Nobel prize winners by last names:")
for winner in L:
print(winner)