Skip to content

Commit 73512c9

Browse files
committed
added shell sort
1 parent 334a054 commit 73512c9

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

allalgorithms/sorting/shell_sort.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Shell Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Elias
7+
# Github: @eliasbayona
8+
#
9+
10+
def shell_sort(arr):
11+
n = len(arr)
12+
h = int(n/2)
13+
14+
15+
while h > 0:
16+
for i in range(h,n):
17+
18+
temp = arr[i]
19+
j = i
20+
21+
while j >= h and arr[j-h] >temp:
22+
arr[j] = arr[j-h]
23+
j -= h
24+
25+
arr[j] = temp
26+
27+
h = int(h/2)
28+
29+
return arr
30+

docs/sorting/shell-sort.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Selection Sort
2+
3+
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.
4+
## Install
5+
6+
```
7+
pip install allalgorithms
8+
```
9+
10+
## Usage
11+
12+
```py
13+
from allalgorithms.sorting import shell_sort
14+
15+
arr = [77, 2, 10, -2, 1, 7]
16+
17+
print(shell_sort(arr))
18+
# -> [-2, 1, 2, 7, 10, 77]
19+
```
20+
21+
## API
22+
23+
### selection_sort(array)
24+
25+
> Returns a sorted array
26+
27+
##### Params:
28+
29+
- `array`: Unsorted Array

tests/test_sorting.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
pigeonhole_sort,
99
stooge_sort,
1010
cocktail_shaker_sort,
11-
tree_sort
11+
tree_sort,
12+
shell_sort,
1213
)
1314

1415

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

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

4144
if __name__ == "__main__":
4245
unittest.main()

0 commit comments

Comments
 (0)
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