VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-for-bitonic-sort/

⇱ Python Program for Bitonic Sort - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Program for Bitonic Sort

Last Updated : 6 Nov, 2025

A sequence is bitonic if it first increases and then decreases. Formally, an array arr[0..n-1] is bitonic if there exists an index i (0 ≤ i ≤ nāˆ’1) such that:

x0 <= x1 …..<= xi and xi >= xi+1….. >= xn-1

  • A fully increasing or fully decreasing sequence is also considered bitonic.
  • Any rotation of a bitonic sequence remains bitonic.

Bitonic Sort is a parallel sorting algorithm that works efficiently when the number of elements is a power of 2.

How Bitonic Sort Works

It mainly works in two steps:

  1. Form a bitonic sequence: divide the array into two halves- one sorted in ascending order and the other in descending order.
  2. Merge: compare and swap elements to form smaller bitonic sequences until the entire array is sorted.

Python Implementation


Output
1 2 3 4 5 6 7 8

Explanation:

  • bitonicSort(a, low, cnt, dire): recursively divides the array and sorts halves in opposite directions to form a bitonic sequence.
  • bitonicMerge(a, low, cnt, dire): compares and swaps elements based on direction to merge the bitonic sequence into sorted order.
  • k = cnt // 2: splits the current sequence into two equal halves.
  • dire == 1 / 0: determines sorting order - 1 for ascending, 0 for descending.
  • sort(a, N, up): wrapper function that initiates bitonic sorting on the full array.

Related Articles:

Comment
Article Tags:
Article Tags: