-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create bitonic_sort.py in /algorithms/sort/ and update test_sort, __i…
…nit__ and all README files (#317) * Create bitonic_sort.py * Update __init__.py * Update test_sort.py * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update bitonic_sort.py * Update bitonic_sort.py * Update bitonic_sort.py * Update test_sort.py * Update test_sort.py * Update bitonic_sort.py * Update bitonic_sort.py * Update bitonic_sort.py * Update bitonic_sort.py * Update bitonic_sort.py
- Loading branch information
Showing
8 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters