Computer Science, asked by nalinidebata, 10 months ago

program to read a list containing 3-digit integers only. Then write an insertion sort program that
sorts the list on the basis of one's digit of all elements. That is, if given list is :
[387, 410, 285, 106]
then the sorted list (as per above condition) should be:
[410, 285, 106, 387]
One's digits are in sorted order (0, 5, 6, 7).
For two matching one's digits, the order remains as in the original list.​

Answers

Answered by anjumanrana
0

Answer:

def insertion_sort(arr):

for i in range(1,len(arr)):

key = arr[i] j = i-1

while j >= 0 and key[-1] < arr[j][-1]: # [-1] is used to take the last digit

arr[j+1] = arr[j] j -= 1

arr[j+1] = key

return arr

l = input("Enter a list of 3 digit numbers : ").split() # split() splits the input on whitespace, here we are not converting the input into int type for the purpose of taking the last digit

print(insertion_sort(l))

# Output

Enter a list of 3 digit numbers : 387 410 285 106

['410', '285', '106', '387']

Similar questions