Insertion Sort - Part 1

  • + 0 comments

    this my python solution :.

    def insertionSort1(n, arr): m = arr[n - 1]

    for j in range(n - 1, 0, -1):
        if arr[j - 1] > m:
            arr[j] = arr[j - 1]
            print(' '.join(str(x) for x in arr))
        else:
            arr[j] = m
            print(' '.join(str(x) for x in arr))
            return  # Exit after inserting
    
    # Insert at the beginning if m is the smallest
    arr[0] = m
    print(' '.join(str(x) for x in arr))