Shell Sort in DSA Python

Program 1

import os

def shell_sort(a):
    n=len(a)
    gap=n//2
    while(gap>=1):
        j=gap
        while(j<n):
            i=j-gap
            while(i>=0):
                if(a[i+gap]<a[i]):
                    a[i+gap],a[i]=a[i],a[i+gap]
                else:
                    break    
                i=i-gap    
            j=j+1
        gap=gap//2
os.system('cls')
mylist=[]
n=int(input("Enter the limit"))
print("Enter elements in list")
for i in range(n):
    x=int(input())
    mylist.append(x)

shell_sort(mylist)

print("Sorted element ")
print(mylist)

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *