Insertion Sort - Part 1

  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'insertionSort1' function below.
    #
    # The function accepts following parameters:
    #  1. INTEGER n
    #  2. INTEGER_ARRAY arr
    #
    
    def insertionSort1(n, arr):
        # Write your code here
        for i in range(n):
            j = i
            curr = arr[i]
            if curr < arr[j-1]:
                while j > 0 and curr < arr[j-1]:
                    arr[j]= arr[j-1]
                    j-=1
                    print(*arr)
                
                if j != i:
                    arr[j] = curr
                    print(*arr)
            
                
    
    if __name__ == '__main__':
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
    
        insertionSort1(n, arr)