Skip to content

Commit

Permalink
Merge d525043 into de286fd
Browse files Browse the repository at this point in the history
  • Loading branch information
hsi1032 committed Jun 7, 2018
2 parents de286fd + d525043 commit eacecda
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ If you want to uninstall algorithms, it is as simple as:
- [randomized_set](algorithms/set/randomized_set.py)
- [set_covering](algorithms/set/set_covering.py)
- [sort](algorithms/sort)
- [bitonic_sort](algorithms/sort/bitonic_sort.py)
- [bogo_sort](algorithms/sort/bogo_sort.py)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pip3 uninstall -y algorithms
- [randomized_set:随机集合](algorithms/set/randomized_set.py)
- [set_covering:集合覆盖](algorithms/set/set_covering.py)
- [sort:排序](algorithms/sort)
- [bitonic_sort](algorithms/sort/bitonic_sort.py)
- [bogo_sort](algorithms/sort/bogo_sort.py)
- [bubble_sort:冒泡排序](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [randomized_set](algorithms/set/randomized_set.py)
- [set_covering](algorithms/set/set_covering.py)
- [sort](algorithms/sort)
- [bitonic_sort](algorithms/sort/bitonic_sort.py)
- [bogo_sort](algorithms/sort/bogo_sort.py)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ if __name__ == "__main__":
- [randomized_set](algorithms/set/randomized_set.py)
- [set_covering](algorithms/set/set_covering.py)
- [sort : ソート](algorithms/sort)
- [bitonic_sort](algorithms/sort/bitonic_sort.py)
- [bogo_sort](algorithms/sort/bogo_sort.py)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ if __name__ == "__main__":
- [randomized_set](algorithms/set/randomized_set.py)
- [set_covering](algorithms/set/set_covering.py)
- [sort : 정렬 알고리즘](algorithms/sort)
- [bitonic_sort](algorithms/sort/bitonic_sort.py)
- [bogo_sort](algorithms/sort/bogo_sort.py)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/sort/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .bitonic_sort import *
from .bogo_sort import *
from .bubble_sort import *
from .comb_sort import *
Expand Down
43 changes: 43 additions & 0 deletions algorithms/sort/bitonic_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
def bitonic_sort(arr, reverse=False):
"""
bitonic sort is sorting algorithm to use multiple process, but this code not containing parallel process
It can sort only array that sizes power of 2
It can sort array in both increasing order and decreasing order by giving argument true(increasing) and false(decreasing)
Worst-case in parallel: O(log(n)^2)
Worst-case in non-parallel: O(nlog(n)^2)
reference: https://en.wikipedia.org/wiki/Bitonic_sorter
"""
def compare(arr, reverse):
n = len(arr)//2
for i in range(n):
if reverse != (arr[i] > arr[i+n]):
arr[i], arr[i+n] = arr[i+n], arr[i]
return arr

def bitonic_merge(arr, reverse):
n = len(arr)

if n <= 1:
return arr

arr = compare(arr, reverse)
left = bitonic_merge(arr[:n // 2], reverse)
right = bitonic_merge(arr[n // 2:], reverse)
return left + right

#end of function(compare and bitionic_merge) definition
n = len(arr)
if n <= 1:
return arr
# checks if n is power of two
if not (n and (not(n & (n - 1))) ):
raise ValueError("the size of input should be power of two")

left = bitonic_sort(arr[:n // 2], True)
right = bitonic_sort(arr[n // 2:], False)

arr = bitonic_merge(left + right, reverse)

return arr
9 changes: 9 additions & 0 deletions tests/test_sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from algorithms.sort import (
bitonic_sort,
bogo_sort,
bubble_sort,
comb_sort,
Expand All @@ -22,6 +23,14 @@ class TestSuite(unittest.TestCase):
def test_bogo_sort(self):
self.assertEqual([1, 5, 23],
bogo_sort([1, 23, 5]))

def test_bitonic_sort(self):
self.assertEqual([1, 2, 3, 5, 23, 57, 65, 1232],
bitonic_sort([1, 3, 2, 5, 65, 23, 57, 1232]))
self.assertEqual([1, 2, 3, 5, 23, 57, 65, 1232],
bitonic_sort([1, 3, 2, 5, 65, 23, 57, 1232],False))
self.assertEqual([1232, 65, 57, 23, 5, 3, 2, 1],
bitonic_sort([1, 2, 3, 5, 65, 23, 57, 1232],True))

def test_bubble_sort(self):
self.assertEqual([1, 5, 23, 57, 65, 1232],
Expand Down

0 comments on commit eacecda

Please sign in to comment.