File size: 546 Bytes
30e4290
 
 
 
 
bd2c426
30e4290
bd2c426
 
 
30e4290
bd2c426
 
 
 
 
 
 
 
 
 
30e4290
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# insertion sort


def insertion_sort(arr):
    steps = []  # keep all steps here
    a = arr.copy()

    for i in range(1, len(a)):
        key = a[i]
        j = i - 1

        while j >= 0 and a[j] > key:
            a[j + 1] = a[j]
            steps.append(
                {"array": a.copy(), "active_index": j + 1, "sorted_boundary": i}
            )
            j -= 1
        a[j + 1] = key
        steps.append(
            {"array": a.copy(), "active_index": j + 1, "sorted_boundary": i}
        )  # save every moment
    return steps