#Recall in the lesson on sorts that we had you complete the #Bubble and Selectio
ID: 3717371 • Letter: #
Question
#Recall in the lesson on sorts that we had you complete the #Bubble and Selection sort, and we showed you Merge sort. 1e didn't show any of insertion sort, and I bet you can #guess why. #implement insertion sort below. Name your function 'insertion'. insertion should take as #input a list, and return as output a sorted list. Note tha #even though technically a sorting method does not have to #return the sorted list, yours should. #If you're stuck on where to start, or having trouble #visualizing or understanding how exactly insertion sort #works, check out this website - https://visualgo.net/sorti #It provides a visual representation of all of the sorting #algorithms as well as pseudocode you can base your own cod #off of. Write your code here! The code below will test your function. If your function #works, it will print: [1, 2, 3, 4, 5]. print(insertion([5, 1, 3, 2, 4]))Explanation / Answer
def insertion(l): for i in range(1, len(l)): temp = l[i] j = i while j > 0 and l[j - 1] > temp: l[j] = l[j - 1] j -= 1 l[j] = temp return l print(insertion([5, 1, 3, 2, 4]))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.