Insertion Sort - Part 2 Solution
In Insertion Sort Part 1, you inserted one element into an array at its correct sorted position. Using the same approach repeatedly, can you sort an entire array?
Guideline: You already can place an element into a sorted array. How can you use that code to build up a sorted array, one element at a time? Note that in the first step, when you consider an array with just the first element, it is already sorted since there's nothing to compare it to.
In this challenge, print the array after each iteration of the insertion sort, i.e., whenever the next element has been inserted at its correct position. Since the array composed of just the first element is already sorted, begin printing after placing the second element.
For example, there are elements in . Working from left to right, we get the following output:3 4 7 5 6 2 1 3 4 7 5 6 2 1 3 4 5 7 6 2 1 3 4 5 6 7 2 1 2 3 4 5 6 7 1 1 2 3 4 5 6 7
Complete the insertionSort2 function in the editor below. At each iteration, it should print the array as space-separated integers on a separate line.
insertionSort2 has the following parameter(s):
- n: an integer representing the length of the array
- arr: an array of integers
The first line contains an integer, , the size of .
The next line contains space-separated integers .
On each line, output the entire array at every iteration.
6 1 4 3 5 6 2
1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6
Skip testing against itself at position . It is sorted.
Test position against position : , no more to check, no change.
Test position against positions and :
- , new position may be . Keep checking.
- , so insert at position and move others to the right.
Test position 3 against positions (as necessary): no change.
Test position 4 against positions : no change.
Test position 5 against positions , insert at position and move others to the right.
Solution in Python
def insertionSort1(n, arr): for n in range(1,n): i = arr[n] while n>0: if i > arr[n-1]: arr[n] = i print(*arr) break else: arr[n] =arr[n-1] n-=1 else: arr = i print(*arr) n = int(input()) arr = list(map(int,input().split())) insertionSort1(n, arr)