Skip to content

Conversation

Siddhram
Copy link
Contributor

This PR introduces a fully documented implementation of Bitonic Sort in R, designed to sort numeric or comparable vectors efficiently using the bitonic sequence approach.

Overview

The bitonic.sort function recursively constructs a bitonic sequence—a sequence that first increases and then decreases—and merges it in the desired direction (ascending or descending). Although Bitonic Sort is typically defined for sequence lengths that are powers of two, this implementation handles arbitrary lengths by padding with +Inf (for ascending) or -Inf (for descending) and removes padding before returning the sorted vector.

Features

  • Sorts numeric or comparable vectors
  • Supports both ascending and descending order
  • Handles arbitrary-length sequences by internal padding
  • Recursively builds bitonic sequences and merges them efficiently
  • Includes helper functions for compare-and-swap and recursive merging
  • Includes example usage at the bottom of the script
  • Follows consistent API style with other R sorting algorithms

Complexity

  • Time Complexity: O(n log² n)
  • Space Complexity: O(n)

Demonstration

Run the included examples to see Bitonic Sort in action:

bitonic.sort(c(3, 7, 4, 8, 6, 2, 1, 5))
# Output: 1 2 3 4 5 6 7 8

bitonic.sort(c(3, 7, 4, 8, 6, 2, 1, 5), ascending = FALSE)
# Output: 8 7 6 5 4 3 2 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant