Python Program to Implement Shell Sorting

01. Example:

def gaps(size):
    
    length = size.bit_length()
    for k in range(length - 1, 0, -1):
        yield 2**k - 1
 
 
def shellSorting(data):
    def insertionSortingWithGap(gap):
        for i in range(gap, len(data)):
            temp = data[i]
            j = i - gap
            while (j >= 0 and temp < data[j]):
                data[j + gap] = data[j]
                j = j - gap
            data[j + gap] = temp
 
    for g in gaps(len(data)):
        insertionSortingWithGap(g)
 
 
list01 = input('Enter the list of numbers: ').split()
list01 = [int(x) for x in list01]

shellSorting(list01)

print('nSorted list: ', end='')
print(list01)

Output:

Enter the list of numbers: 320 420 140 500 185 60 200 80 10 100
Sorted list: [10, 60, 80, 100, 140, 185, 200, 320, 420, 500]

 

Leave a comment