My QA Projects

QA Projects I was involded.

View on GitHub

Insertion Sort with list

def insertion_sort(arr):
    # Traverse through 1 to len(arr) elements
    # Iterate through each element in the array starting from the second element
    for i in range(1, len(arr)):
        key = arr[i]
        
        # Move elements of arr[0..i-1], that are greater than key,
        # to one position ahead of their current position
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        # Place the key in its correct location
        arr[j + 1] = key

# Example
arr = [12, 11, 13, 5, 6]
insertion_sort(arr)
print("Sorted array:", arr)

Step 1.

Step 2.

Step 3.

Step 4.

Step 5.

Walkthrough for the array [12, 11, 13, 5, 6]

First iteration (i = 1):
Key = 11
Compare 11 with 12 (arr[0]), shift 12 to the right
Place 11 at arr[0]
Result: [11, 12, 13, 5, 6]

Second iteration (i = 2):
Key = 13
Compare 13 with 12 (arr[1]), no shift needed
Result: [11, 12, 13, 5, 6]

Third iteration (i = 3):
Key = 5
Compare 5 with 13 (arr[2]), shift 13 to the right
Compare 5 with 12 (arr[1]), shift 12 to the right
Compare 5 with 11 (arr[0]), shift 11 to the right
Place 5 at arr[0]
Result: [5, 11, 12, 13, 6]

Fourth iteration (i = 4):
Key = 6
Compare 6 with 13 (arr[3]), shift 13 to the right
Compare 6 with 12 (arr[2]), shift 12 to the right
Compare 6 with 11 (arr[1]), shift 11 to the right
Place 6 at arr[1]
Result: [5, 6, 11, 12, 13]