# Odd-Even Transposition Sort

### Sorting in programing:

Sorting is one of the slowest operations in programing. Because many programs rely highly on sorting, much attention is given to finding algorithms which will sort very very fast. Because most sorts are SIMD, (e.g. Bubble Sort - compares & switches, compares & switches,...), This makes a perfect candidate for converting to parallelism.

### Odd-Even Transposition Sort:

Odd-Even Transposition Sort is a parallel sorting algorithm. It is based on the Bubble Sort technique of comparing two numbers and switching them if the first is greater than the second, to achieve a left to right ascending ordering. So what if each number were a PE, (Processing Element)? Each number would look to it's right neighbor and if it were greater, it would switch them. If every PE is doing this in parallel, there is going to be chaos because there will be multiple writes of different data to the same memory location.

### Chaos without Synchronization

The solution is to break the problem into two phases per iteration. In phase one, a PE says, "am I odd or even?" if it is odd, it is active and if it is even, it stands down this phase. The odd PEs look to their right and switch if necessary. In phase two, the same happens but with the even PEs being active. By doing this, there are no concurrent writes to the same memory location.

### Example:

 Iteration Phase PE1 PE2 PE3 PE4 PE5 1 1 4 5 2 1 3 1 2 4 5 1 2 3 2 1 4 1 5 2 3 2 2 1 4 2 5 3 3 1 1 2 4 3 5 3 2 1 2 3 4 5

### Analysis:

Odd-Even Transposition Sort only requires n/2 iterations of the dual phase sort, compared to n passes through the array with Bubble Sort. This algorithm has excellent load balancing because every processor is used once per iteration. Assuming our array of n elements to sort is very large, we will be working with many virtual processors on the same processor to emulate one PE per element. To optimize the code further, one may use a Quick Sort or similar sort on each physical processor to sort the elements it is responsible for. Code examples are avaliable in our class Parallaxis manual, Parallaxis-III: A Language for Structured Data-Parallel Programming, by Thomas Braunl.

The author of this page, Aaron Michalove, wishes to recognize the use of, The Odd-Even Transposition Sort: An Example of Program Development, by Nan C. Scaller.