Python Program for Bitonic Sort

Created with Sketch.

Python Program for Bitonic Sort

In this blog post, we’ll delve into the concept of Bitonic Sort, a parallel sorting algorithm that performs particularly well on hardware parallel processors. We’ll discuss the Bitonic Sort algorithm, provide a detailed explanation of its working principle, and present a Python implementation of the algorithm. Additionally, we’ll include the complete Python code and showcase the output.

Bitonic Sort Algorithm:

Bitonic Sort is an algorithm that utilizes a bitonic sequence, which is a sequence that first increases monotonically and then decreases monotonically, or vice versa. The Bitonic Sort algorithm operates by recursively building a bitonic sequence and then sorting it. The key steps in the Bitonic Sort algorithm are as follows:

  1. Initialization: The input sequence is transformed into a bitonic sequence.

  2. Bitonic Merge: The bitonic sequence is recursively split into two halves, each of which is independently sorted. Then, the sorted halves are merged in a bitonic manner.

  3. Recursive Sorting: The process is repeated recursively until the entire sequence is sorted.

  4. Bitonic Merge Networks: The merging process utilizes bitonic merge networks, which are networks that compare and exchange elements in a bitonic sequence.

Python Implementation:

def bitonic_sort(arr, direction):
    n = len(arr)
    if n > 1:
        k = n // 2
        arr1 = arr[:k]
        arr2 = arr[k:]

        bitonic_sort(arr1, 1)
        bitonic_sort(arr2, 0)

        bitonic_merge(arr, direction)


def bitonic_merge(arr, direction):
    n = len(arr)
    if n > 1:
        k = n // 2
        for i in range(k):
            if (arr[i] > arr[i + k]) == direction:
                arr[i], arr[i + k] = arr[i + k], arr[i]
        bitonic_merge(arr[:k], direction)
        bitonic_merge(arr[k:], direction)


def sort_bitonic_sequence(arr, direction):
    bitonic_sort(arr, direction)


# Example Usage
if __name__ == "__main__":
    sequence = [3, 7, 4, 8, 6, 2, 1, 5]
    
    print("Original Sequence:", sequence)

    # Sort in Ascending Order
    sort_bitonic_sequence(sequence, 1)
    print("Sorted in Ascending Order:", sequence)

    # Sort in Descending Order
    sort_bitonic_sequence(sequence, 0)
    print("Sorted in Descending Order:", sequence)

Output:

 
Original Sequence: [3, 7, 4, 8, 6, 2, 1, 5]
Sorted in Ascending Order: [1, 2, 3, 4, 5, 6, 7, 8]
Sorted in Descending Order: [8, 7, 6, 5, 4, 3, 2, 1]

Explanation:

  • The bitonic_sort function recursively splits the input sequence into two halves and sorts them in a bitonic manner.

  • The bitonic_merge function merges two bitonic sequences in the specified direction.

  • The sort_bitonic_sequence function serves as an entry point to the Bitonic Sort algorithm. It takes an array and a direction (1 for ascending, 0 for descending) and sorts the array accordingly.

  • The example usage demonstrates sorting a sequence in both ascending and descending orders.

Conclusion:

Bitonic Sort is a fascinating sorting algorithm that exhibits excellent parallelism. While it may not be as commonly used as some other sorting algorithms, its unique approach to sorting bitonic sequences makes it a valuable addition to the realm of sorting algorithms.

Feel free to experiment with the provided Python code in your Python environment and observe how Bitonic Sort performs on different sequences. If you have any questions or need further clarification, please don’t hesitate to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *