Skip to content

added shell sort #27

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

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions allalgorithms/sorting/shell_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: UTF-8 -*-
#
# Shell Sort Algorithm
# The All ▲lgorithms library for python
#
# Contributed by: Elias
# Github: @eliasbayona
#

def shell_sort(arr):
n = len(arr)
h = int(n/2)


while h > 0:
for i in range(h,n):

temp = arr[i]
j = i

while j >= h and arr[j-h] >temp:
arr[j] = arr[j-h]
j -= h

arr[j] = temp

h = int(h/2)

return arr

29 changes: 29 additions & 0 deletions docs/sorting/shell-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Selection Sort

In computer science, shell sort improves upon insertion sort by moving out of order elements more than one position at a time. It has a best case O(n log n) time complexity, and for other cases, it depends on the gap sequence. According to Poonen Theorem, worst case complexity for shell sort is Θ(NlogN)^2/(log logN)^2) or Θ(NlogN)^2/log logN) or Θ(N(logN)^2) or something in between.
## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.sorting import shell_sort

arr = [77, 2, 10, -2, 1, 7]

print(shell_sort(arr))
# -> [-2, 1, 2, 7, 10, 77]
```

## API

### selection_sort(array)

> Returns a sorted array

##### Params:

- `array`: Unsorted Array
5 changes: 4 additions & 1 deletion tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
pigeonhole_sort,
stooge_sort,
cocktail_shaker_sort,
tree_sort
tree_sort,
shell_sort,
)


Expand Down Expand Up @@ -37,6 +38,8 @@ def test_cocktail_shaker_sort(self):
def tree_sort(self):
self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1]))

def test_shell_sort(self):
self.assertEqual([-44, 1, 2, 3, 7, 19], shell_sort([7, 3, 2, 19, -44, 1]))

if __name__ == "__main__":
unittest.main()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy