Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita8ebdc7

Browse files
authored
Merge pull requestabranhe#12 from martmists/master
Add Stooge, pidgeonhole and cocktail shaker sort
2 parents0217d88 +6574504 commita8ebdc7

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_sortimportinsertion_sort
33
from .selection_sortimportselection_sort
44
from .bubble_sortimportbubble_sort
5+
from .pidgeonhole_sortimportpidgeonhole_sort
6+
from .stooge_sortimportstooge_sort
7+
from .cocktail_shaker_sortimportcocktail_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+
defcocktail_shaker_sort(data):
12+
upper=len(data)-1
13+
lower=0
14+
15+
no_swap=False
16+
whileupper-lower>1andnotno_swap:
17+
no_swap=True
18+
forjinrange(lower,upper):
19+
ifdata[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+
forjinrange(upper,lower,-1):
25+
ifdata[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+
returndata
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+
defpidgeonhole_sort(data):
12+
minimum=min(data)
13+
size=max(data)-minimum+1
14+
holes= [0]*size
15+
foritemindata:
16+
holes[item-minimum]+=1
17+
i=0
18+
forcountinrange(size):
19+
whileholes[count]>0:
20+
holes[count]-=1
21+
data[i]=count+minimum
22+
i+=1
23+
returndata
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+
defstooge_sort(seq,start=0,end=None):
12+
ifendisNone:
13+
end=len(seq)-1
14+
ifseq[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+
returnseq
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.sortingimport 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.sortingimport 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.sortingimport 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
importunittest
22

33
fromallalgorithms.sortingimport (
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
classTestSorting(unittest.TestCase):
1115
deftest_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
deftest_selection_sort(self):
2125
self.assertEqual([-44,1,2,3,7,19],selection_sort([7,3,2,19,-44,1]))
2226

27+
deftest_pidgeonhole_sort(self):
28+
self.assertEqual([-44,1,2,3,7,19],pidgeonhole_sort([7,3,2,19,-44,1]))
29+
30+
deftest_stooge_sort(self):
31+
self.assertEqual([-44,1,2,3,7,19],stooge_sort([7,3,2,19,-44,1]))
32+
33+
deftest_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)

[8]ページ先頭

©2009-2025 Movatter.jp