Bitonic sequence 2026-10-25
A sequence formed by an ascending part followed by a descending part, or the reverse arrangement, with either part possibly empty. In sorting algorithms, the monotonicity convention normally allows equal keys. Joining one ascending sequence to the reversal of another ascending sequence produces a bitonic sequence.
Bitonic sorting algorithm 2026-10-24
A divide-and-conquer algorithm that sorts two halves, reverses one result to form a bitonic sequence, then performs a bitonic merge. For the shuffle-and-adjacent-comparison merging network on entries, passes each cost , so merging costs . The complete recurrence relation is , giving time. A compare-exchange operation preserves the multiset of entries while putting its two selected entries in order.
Apply the supplied bitonic-merging fact directly:For length ,
merge :: Ord a => [a] -> [a]
merge xs = rounds (levels (length xs)) xs
where
levels n
| n <= 1 = 0
| otherwise = 1 + levels (n `div` 2)
order (x,y)
| x <= y = (x,y)
| otherwise = (y,x)
pass = unpairs . map order . pairs . riffle
rounds 0 ys = ys
rounds r ys = rounds (r-1) (pass ys)levels n = k. Each pass performs exactly the required shuffle and adjacent compare-exchange operations. The supplied fact therefore proves that rounds k yields ascending order from a bitonic sequence. Each pass preserves the multiset of entries and costs , giving time for . Length one needs no pass. The definition also harmlessly returns [] on empty input.Sort the two halves ascending, reverse the second, and concatenate to form a bitonic sequence:For a power-of-two length greater than one, both halves are smaller powers of two. By induction, the recursive calls return ascending permutations of their respective inputs. Reversal makes the second half descending, so part (e)'s precondition holds and
sort :: Ord a => [a] -> [a]
sort [] = []
sort [x] = [x]
sort xs = merge (sort as ++ reverse (sort bs))
where (as,bs) = halve xsmerge sorts their concatenation without losing entries. This proves both sortedness and sorting permutation preservation. Its recurrence relation is , so summing the levels gives time. Equal keys cause no difficulty; ascending and descending here permit equality. This is a bitonic sorting algorithm.