+ 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.
6 odpowiedzi
+ 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
+ 1
Can we not do it by only using for, while, if.?
+ 1
the second example uses loops for and while
+ 1
But it also uses def
+ 1
thata used to define the function so we can call the function when needed elsewhere
+ 1
Ok thanks