Skip to content

Commit a8ebdc7

Browse files
authored
Merge pull request abranhe#12 from martmists/master
Add Stooge, pidgeonhole and cocktail shaker sort
2 parents 0217d88 + 6574504 commit a8ebdc7

File tree

10 files changed

+188
-6
lines changed

10 files changed

+188
-6
lines changed

.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
root = true
99

1010
[*]
11-
indent_style = tab
11+
indent_style = space
12+
indent_size = 4
1213
end_of_line = lf
1314
charset = utf-8
1415
trim_trailing_whitespace = true

allalgorithms/.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
root = true
88

99
[*]
10-
indent_style = tab
10+
indent_style = space
11+
indent_size = 4
1112
end_of_line = lf
1213
charset = utf-8
1314
trim_trailing_whitespace = true

allalgorithms/sorting/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
from .insertion_sort import insertion_sort
33
from .selection_sort import selection_sort
44
from .bubble_sort import bubble_sort
5+
from .pidgeonhole_sort import pidgeonhole_sort
6+
from .stooge_sort import stooge_sort
7+
from .cocktail_shaker_sort import cocktail_shaker_sort
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Cocktail Shaker Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
def cocktail_shaker_sort(data):
12+
upper = len(data) - 1
13+
lower = 0
14+
15+
no_swap = False
16+
while upper - lower > 1 and not no_swap:
17+
no_swap = True
18+
for j in range(lower, upper):
19+
if data[j + 1] < data[j]:
20+
data[j + 1], data[j] = data[j], data[j + 1]
21+
no_swap = False
22+
upper = upper - 1
23+
24+
for j in range(upper, lower, -1):
25+
if data[j - 1] > data[j]:
26+
data[j - 1], data[j] = data[j], data[j - 1]
27+
no_swap = False
28+
lower = lower + 1
29+
30+
return data
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Pidgeonhole Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
def pidgeonhole_sort(data):
12+
minimum = min(data)
13+
size = max(data) - minimum + 1
14+
holes = [0] * size
15+
for item in data:
16+
holes[item - minimum] += 1
17+
i = 0
18+
for count in range(size):
19+
while holes[count] > 0:
20+
holes[count] -= 1
21+
data[i] = count + minimum
22+
i += 1
23+
return data

allalgorithms/sorting/stooge_sort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Stooge Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
def stooge_sort(seq, start=0, end=None):
12+
if end is None:
13+
end = len(seq) - 1
14+
if seq[start] > seq[end]:
15+
seq[start], seq[end] = seq[end], seq[start]
16+
if (end - start + 1) > 2:
17+
third = (end - start + 1) // 3
18+
stooge_sort(seq, start, end-third)
19+
stooge_sort(seq, start+third, end)
20+
stooge_sort(seq, start, end-third)
21+
return seq

docs/sorting/cocktail-shaker-sort.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Cocktail Shaker Sort
2+
3+
Cocktail shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts. It provides only marginal performance improvements, and does not improve asymptotic performance; like the bubble sort, it is not of practical interest (insertion sort is preferred for simple sorts), though it finds some use in education.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.sorting import cocktail_shaker_sort
15+
16+
arr = [77, 2, 10, -2, 1, 7]
17+
18+
print(cocktail_shaker_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
## API
23+
24+
### cocktail_shaker_sort(array)
25+
26+
> Returns a sorted array
27+
28+
##### Params:
29+
30+
- `array`: Unsorted Array

docs/sorting/pidgeonhole-sort.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Pidgeonhole Sort
2+
3+
Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the length of the range of possible key values (N) are approximately the same. It requires O(n + N) time. It is similar to counting sort, but differs in that it "moves items twice: once to the bucket array and again to the final destination [whereas] counting sort builds an auxiliary array then uses the array to compute each item's final destination and move the item there."
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.sorting import pidgeonhole_sort
15+
16+
arr = [77, 2, 10, -2, 1, 7]
17+
18+
print(pidgeonhole_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
## API
23+
24+
### pidgeonhole_sort(array)
25+
26+
> Returns a sorted array
27+
28+
##### Params:
29+
30+
- `array`: Unsorted Array

docs/sorting/stooge-sort.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Stooge Sort
2+
3+
Stooge sort is a recursive sorting algorithm. It is notable for its exceptional bad time complexity of O(n^(log 3 / log 1.5)) = O(n^2.7095...). The running time of the algorithm is thus slower compared to reasonable sorting algorithms, and is slower than Bubble sort, a canonical example of a fairly inefficient sort. It is however more efficient than Slowsort.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.sorting import stooge_sort
15+
16+
arr = [77, 2, 10, -2, 1, 7]
17+
18+
print(stooge_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
## API
23+
24+
### stooge_sort(array)
25+
26+
> Returns a sorted array
27+
28+
##### Params:
29+
30+
- `array`: Unsorted Array

tests/test_sorting.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import unittest
22

33
from allalgorithms.sorting import (
4-
bubble_sort,
5-
insertion_sort,
6-
merge_sort,
7-
selection_sort
4+
bubble_sort,
5+
insertion_sort,
6+
merge_sort,
7+
selection_sort,
8+
pidgeonhole_sort,
9+
stooge_sort,
10+
cocktail_shaker_sort
811
)
912

13+
1014
class TestSorting(unittest.TestCase):
1115
def test_merge_sort(self):
1216
self.assertEqual([-44, 1, 2, 3, 7, 19], merge_sort([7, 3, 2, 19, -44, 1]))
@@ -20,6 +24,15 @@ def test_insertion_sort(self):
2024
def test_selection_sort(self):
2125
self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1]))
2226

27+
def test_pidgeonhole_sort(self):
28+
self.assertEqual([-44, 1, 2, 3, 7, 19], pidgeonhole_sort([7, 3, 2, 19, -44, 1]))
29+
30+
def test_stooge_sort(self):
31+
self.assertEqual([-44, 1, 2, 3, 7, 19], stooge_sort([7, 3, 2, 19, -44, 1]))
32+
33+
def test_cocktail_shaker_sort(self):
34+
self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1]))
35+
2336

2437
if __name__ == "__main__":
2538
unittest.main()

0 commit comments

Comments
 (0)