Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell Sort in_python #154

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Sorting/Shell Sort/in_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#Implementation of Shell sort

'''
It is a sorting algorithm that is an extended version of insertion sort.
Shell sort has improved the average time complexity of insertion sort.
As similar to insertion sort, it is a comparison-based and in-place sorting algorithm.
Shell sort is efficient for medium-sized data sets. In insertion sort, we move elements only one position ahead.
When an element has to be moved far ahead, many movements are involved. The idea of ShellSort is to allow the exchange of far items.
In Shell sort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1.
An array is said to be h-sorted if all sublists of every h’th element are sorted.
'''

#Time Complexity
#->Best Case O(n*logn)
#->Average Case O(n*log(n)2)
#->Worst Case O(n2)

#Space Complexity O(1)
#Stable N0




# function for shell sort
def shellsort(MyList):
n = len(MyList)
gap = n // 2
while gap > 0:
for i in range(gap,n):
temp = MyList[i]
j = i
while j >= gap and MyList[j-gap] > temp:
MyList[j] = MyList[j-gap]
j = j - gap
MyList[j] = temp
gap = gap // 2

# function to print list
def PrintList(MyList):
for i in MyList:
print(i, end=" ")
print("\n")

# test the code
MyList = [10, 1, 23, 50, 4, 9, -4]
print("Original List")
PrintList(MyList)

shellsort(MyList)
print("Sorted List")
PrintList(MyList)