i sort Algorithm

The i-sort algorithm, also known as the insertion sort algorithm, is a simple yet efficient sorting technique employed in computer science and programming for arranging a list or an array of elements in a particular order - either ascending or descending. The algorithm functions by iterating through the list, comparing each element with its adjacent elements, and swapping them if they are out of order. It is highly useful for organizing small and partially sorted lists, and is often incorporated as a part of more complex sorting algorithms such as the merge sort and the quick sort. The primary advantage of the i-sort algorithm lies in its simplicity and ease of implementation, as it does not require any additional storage space and has a low overhead. However, it is not the most efficient algorithm for sorting large lists or arrays, as its worst-case time complexity is O(n^2), where n represents the number of elements in the list. Despite this limitation, i-sort remains popular for its intuitive nature and its ability to perform well in specific scenarios, such as when the list is already partially sorted or when the number of elements is relatively small.
def insertionSort(arr):
    """
    >>> a = arr[:]
    >>> insertionSort(a)
    >>> a == sorted(a)
    True
    """
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key


arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print("Sorted array is:")
for i in range(len(arr)):
    print("%d" % arr[i])

LANGUAGE:

DARK MODE: