Data Structures and Algorithms

Sorting methods with a fixed sequence of compare-exchange steps (sorting networks), such as bitonic sort, are sometimes used for sorting on parallel hardware such as GPUs. Why?

  1. AThey make O(n) comparisons, fewer than the O(n log n) lower bound for comparison sorting
  2. BThey are stable sorts that keep elements with equal keys in their original order
  3. CWhich pairs of elements are compared and swapped is fixed in advance regardless of the data values, so many threads can work in the same steps at the same time with uniform branching
Show answer and explanation

Answer: C. Which pairs of elements are compared and swapped is fixed in advance regardless of the data values, so many threads can work in the same steps at the same time with uniform branching

In bitonic sort, the pairs compared at each stage are determined by their indices alone, and all compare-exchange operations within a stage can run at the same time. n = 2ᵏ items are sorted in k(k+1)/2 stages: 16 items take 10 stages and 80 comparisons in total, and 1,024 items take 55 stages and 28,160 comparisons. The total number of comparisons is O(n log² n), more than quicksort and similar algorithms, but because the steps do not depend on the data, devices like GPUs, where many threads execute the same instructions, run them efficiently without divergent branches. No method that sorts by comparisons alone can use fewer than O(n log n) comparisons, and bitonic sort is generally not stable either.

  • A. No comparison sort can do better than O(n log n) comparisons; bitonic sort makes O(n log² n) comparisons in total.
  • B. Bitonic sort also swaps elements that are far apart, so it is generally not stable.
  • C. Correct. Because the steps do not depend on the data, many threads can process them simultaneously with uniform branching.
Practice this topic Back to topic

Related calculators

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

More Data Structures and Algorithms questions

  1. For an array of n elements, what is the time complexity of comparing every pair of distinct elements exactly once (a double loop that compares element i with every element after it, advancing i one step at a time)?
  2. A program with O(n²) time complexity takes 0.5 seconds for 1,000 data items. About how long will it take for 4,000 items?
  3. A linear search compares the elements of an array of 100 distinct values one by one from the front to find a target. If the target is always in the array and is equally likely to be at any position, what is the average number of comparisons?
  4. Compared with an array, which is a correct description of a linked list (a list in which each element holds a pointer to the next element)?
  5. On an empty stack, the operations push 1, push 2, pop, push 3, push 4, pop, pop are performed in this order. In what order are the values removed by pop?