Q: Help with insertion Sort Algorithm in Python. Details if can to better unders
ID: 3823297 • Letter: Q
Question
Q: Help with insertion Sort Algorithm in Python. Details if can to better understand, Thanks for the help ahead.
Description of the Insertion Sort algorithm1 : The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray, which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. Following example explains above steps:
arr = [64 25 12 22 11]
// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64
// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11 12 25 22 64
// Find the minimum element in arr[2...4]
// and place it at beginning of arr[2...4]
11 12 22 25 64
// Find the minimum element in arr[3...4]
// and place it at beginning of arr[3...4]
11 12 22 25 64
I have to use In the code file the following function, which receives and sorts list lst (the function does not return anything): insertion_sort(lst).
Explanation / Answer
def insertionSort(list):
for index in range(1,len(list)):
currentvalue = list[index]
position = index
while position>0 and list[position-1]>currentvalue:
list[position]=list[position-1]
position = position-1
list[position]=currentvalue
print(list)
list = [64,25,12,22,11]
insertionSort(list)
print(list)
hi
above code does exactly what you are looking for.
output of the above function shows all the passes of insertion Sort.
hope you understand this. if you still have doubt you can comment here and ask your doubts.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.