Given following list l=[("Wilhelm roentgen", "physics", 1901),("Marie Curie", "physics", 1903),("Ivan pavlov", "medicine", 1904) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Given following list l=[("Wilhelm roentgen", "physics", 1901),("Marie Curie", "physics", 1903),("Ivan pavlov", "medicine", 1904)

]. Write program to sort list in order of last names using insertion sort.

10th Feb 2019, 2:02 PM
Agrim Yadava
Agrim Yadava - avatar
6 Answers
+ 1
think and it will become clear. Use the [insort][1] function of the bisect module would be one way >> import bisect >> a = [1, 2, 4, 5] >> bisect.insort(a, 3) >> print(a) [1, 2, 3, 4, 5] shifting vs exchanging is an area you are bordering on. another way ... def insertionSort(alist): for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue alist = [54,26,93,17,77,31,44,55,20] insertionSort(alist) print(alist) try them its a coding playground as thats where we can play with codes lol
10th Feb 2019, 2:15 PM
peter
peter - avatar
+ 1
Can we not do it by only using for, while, if.?
10th Feb 2019, 2:18 PM
Agrim Yadava
Agrim Yadava - avatar
+ 1
the second example uses loops for and while
10th Feb 2019, 2:23 PM
peter
peter - avatar
+ 1
But it also uses def
10th Feb 2019, 2:24 PM
Agrim Yadava
Agrim Yadava - avatar
+ 1
thata used to define the function so we can call the function when needed elsewhere
10th Feb 2019, 2:25 PM
peter
peter - avatar
+ 1
Ok thanks
10th Feb 2019, 2:28 PM
Agrim Yadava
Agrim Yadava - avatar