FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-python/algorithms/sort/bitonic_sort.py at master · ivan1911/algorithms-python · GitHub
ivan1911
/
algorithms-python
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms-python
/
algorithms
/
sort
/
bitonic_sort.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (34 loc) · 1.37 KB
Breadcrumbs
algorithms-python
/
algorithms
/
sort
/
bitonic_sort.py
Copy path
File metadata and controls
43 lines (34 loc) · 1.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
Back
|
FazBrowse Home
|
New Git URL