日本語

PARALLEL-FRIENDLY STRUCTURES

Odd-Even Sort, visualised

Odd-even sort (odd-even transposition sort) alternately compares and swaps odd-indexed and even-indexed pairs. Pairs in the same phase never overlap, which makes the structure suitable for parallel processing.

Run Odd-Even Sort in the visualizer →

Opens the Sorting Cinema visualizer with 16 elements in English.

How it works

  1. Compare pairs starting at odd positions (1,2), (3,4)… and swap if the left one is larger.
  2. Do the same for pairs starting at even positions (0,1), (2,3)…
  3. Repeat both phases until a round makes no swaps.

Complexity and stability

Best
O(n)
Average
O(n²)
Worst
O(n²)
Extra space
O(1)
Stability
Stable

Sorted input finishes after one round, O(n); in general it relies on neighbour swaps, O(n²). This app animates each pair sequentially, so it does not measure performance on parallel hardware.

Source of facts: Complexity is derived from the steps of this app's implementation (src/SortingCinema.jsx).

Trace on a small array

The first operations on the input [5, 2, 4, 1, 3], following the same steps as the app.

#OperationArray after
1Compare [1]=2 and [2]=4[5, 2, 4, 1, 3]
2Compare [3]=1 and [4]=3[5, 2, 4, 1, 3]
3Compare [0]=5 and [1]=2[5, 2, 4, 1, 3]
4Swap [0]=5 and [1]=2[2, 5, 4, 1, 3]
5Compare [2]=4 and [3]=1[2, 5, 4, 1, 3]
6Swap [2]=4 and [3]=1[2, 5, 1, 4, 3]
7Compare [1]=5 and [2]=1[2, 5, 1, 4, 3]
8Swap [1]=5 and [2]=1[2, 1, 5, 4, 3]

…and so on. In this trace, 23 compare / swap / write operations in total produce [1, 2, 3, 4, 5].

Suitable inputs and caveats

  • You can see that pairs in the same phase are independent and could be processed in parallel.
  • As the learning guide notes, this app animates them one after another and does not show parallel speed.
  • It swaps neighbours only when strictly out of order, so it is stable.

FAQ

Why is it called parallel-friendly?

Pairs compared in the same phase do not overlap, so they could all be processed at once. This app shows them one pair at a time.

How is it different from bubble sort?

Both swap neighbours, but odd-even sort alternates between odd-start and even-start pairs, and the pairs within each phase are independent.