Shell Sort Algorithm in Python

In this source code example, we will write a code to implement the Insertion Sort algorithm in Python.

The shell sort algorithm sorts a pair of elements that are not in sequence in a collection. The distance between the elements to be compared is decreased sequentially. This algorithm performs more operations and has a greater cache miss ratio than the quick sort algorithm.

Shell Sort Algorithm in Python

In this Python program, we will take input from the User or console and print the result to the output:
def shell_sort(collection):

    gaps = [701, 301, 132, 57, 23, 10, 4, 1]
    for gap in gaps:
        for i in range(gap, len(collection)):
            insert_value = collection[i]
            j = i
            while j >= gap and collection[j - gap] > insert_value:
                collection[j] = collection[j - gap]
                j -= gap
            if j != i:
                collection[j] = insert_value
    return collection

if __name__ == "__main__":
    user_input = input("Enter numbers separated by a comma:\n").strip()
    unsorted = [int(item) for item in user_input.split(",")]


Enter numbers separated by a comma:
[1, 2, 3, 5, 10, 15, 20]