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)
| 39 | |
| 40 | |
| 41 | def 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 | |
| 65 | def bitonic_sort(array: list[int], low: int, length: int, direction: int) -> None: |
no test coverage detected