forked from theutpal01/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitonic_Sort.py
More file actions
34 lines (25 loc) · 735 Bytes
/
Copy pathBitonic_Sort.py
File metadata and controls
34 lines (25 loc) · 735 Bytes
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
def compAndSwap(a, i, j, dire):
if (dire == 1 and a[i] > a[j]) or (dire == 0 and a[i] > a[j]):
a[i], a[j] = a[j], a[i]
def bitonicMerge(a, low, cnt, dire):
if cnt > 1:
k = cnt//2
for i in range(low, low+k):
compAndSwap(a, i, i+k, dire)
bitonicMerge(a, low, k, dire)
bitonicMerge(a, low+k, k, dire)
def bitonicSort(a, low, cnt, dire):
if cnt > 1:
k = cnt//2
bitonicSort(a, low, k, 1)
bitonicSort(a, low+k, k, 0)
bitonicMerge(a, low, cnt, dire)
def sort(a, N, up):
bitonicSort(a, 0, N, up)
a = [3, 7, 4, 8, 6, 2, 1, 5]
n = len(a)
up = 1
sort(a, n, up)
print("Sorted array is")
for i in range(n):
print("%d" % a[i], end=" ")