MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / bitonic_merge

Function bitonic_merge

sorts/bitonic_sort.py:41–62  ·  view source on GitHub ↗

It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in descending if direction = 0. The sequence to be sorted starts at index position low, the parameter length is the number of elements to be sorted. >>> arr = [12, 42, -21, 1] >>> bitonic_merg

(array: list[int], low: int, length: int, direction: int)

Source from the content-addressed store, hash-verified

39
40
41def bitonic_merge(array: list[int], low: int, length: int, direction: int) -> None:
42 """
43 It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in
44 descending if direction = 0.
45 The sequence to be sorted starts at index position low, the parameter length is the
46 number of elements to be sorted.
47
48 >>> arr = [12, 42, -21, 1]
49 >>> bitonic_merge(arr, 0, 4, 1)
50 >>> arr
51 [-21, 1, 12, 42]
52
53 >>> bitonic_merge(arr, 0, 4, 0)
54 >>> arr
55 [42, 12, 1, -21]
56 """
57 if length > 1:
58 middle = int(length / 2)
59 for i in range(low, low + middle):
60 comp_and_swap(array, i, i + middle, direction)
61 bitonic_merge(array, low, middle, direction)
62 bitonic_merge(array, low + middle, middle, direction)
63
64
65def bitonic_sort(array: list[int], low: int, length: int, direction: int) -> None:

Callers 2

bitonic_sortFunction · 0.85
bitonic_sort.pyFile · 0.85

Calls 1

comp_and_swapFunction · 0.85

Tested by

no test coverage detected