Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Sorting/Shell Sort/Images/shell_sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions Sorting/Shell Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Package/Script Name

## Aim

The main aim of the script is to sort numbers in list using shell sort.


## Purpose
The main purpose is to sort list of any numbers in O(n^2) time complexity.


## Short description of package/script

In shellSort, 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 is sorted.


## Workflow of the Project

Gap is reduce by half in every iteration.
In this case, we begin with n2n2 sublists. On the next pass, n4n4 sublists are sorted. Eventually, a single list is sorted with the basic insertion sort.

## Setup instructions

Install latest version of python from [python](https://www.python.org/downloads/)


## Compilation Steps

Open editor and open the file and compile the program and give input and get the desired output.


## Sample Test Cases
<h3>TEST CASE-1</h3>
<h4>Input:</h4>
1 98 3 16 3
<h5>Output:</h5>
1 3 3 16 98

<h3>TEST CASE-2</h3>
<h4>Input:</h4>
5 4 3 2 1
<h5>Output:</h5>
1 2 3 4 5

<h3>TEST CASE-3</h3>
<h4>Input:</h4>
1 1 1 0 0
<h5>Output:</h5>
0 0 1 1 1

## Output

![](https://github.com/thejaswin123/PyAlgo-Tree/blob/main/Sorting/Shell%20Sort/Images/shell_sort.png)

## Author(s)

[Thejaswin S](https://www.github.com/thejaswin123)

44 changes: 44 additions & 0 deletions Sorting/Shell Sort/shell_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
''' This program illustrates the shell sort implementation in Python

Shell sort or Shell's method, is an in-place comparison sort.
It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by
insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other,
then progressively reducing the gap between elements to be compared. Starting with far apart elements
can move some out-of-place elements into position faster than a simple nearest neighbor exchange.

Best Case O(n logn); Average Case O(depends on gap sequence); Worst Case O(n^2)'''



def shell_sort(arr): #function for shell sort
sublistCount = int(len(arr)/2)

while sublistCount > 0:
for start in range(sublistCount):
do_insertsort_gap(arr,start,sublistCount)
sublistCount = int(sublistCount/2)
return arr

def do_insertsort_gap(arr,start,gap):
for i in range(start+gap,len(arr),gap):
currentValue = arr[i]
position = i
while position >= gap and arr[position-gap] >= currentValue:
arr[position] = arr[position-gap] # We compare values in each sub-list and swap them (if necessary) in the original array.
position = position-gap
arr[position] = currentValue

array = input("Enter the elements of the array separated by space : ") #Taking input
array = [int(x) for x in array.split(" ")] #string to int conversion

print(f"Array after sorting is : {shell_sort(array)}") #printing the output


# sample Test case
""" >>> shell_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> shell_sort([])
[]
>>> shell_sort([-2, -5, -45])
[-45, -5, -2]
"""