From Wakapon
Jump to: navigation, search
Line 18: Line 18:
 
It's a log2(N) algorithm where we recursively compare and swap elements 2 by 2 in a very specific manner.
 
It's a log2(N) algorithm where we recursively compare and swap elements 2 by 2 in a very specific manner.
  
For 8 values, we first perform 4 compare&swaps like this:
+
For 8 values, we first perform 4 compare&swaps like this (pass 1):
  
 
[[File:BitonicBuild0.png]]
 
[[File:BitonicBuild0.png]]
  
Then we perform 4 compares&swaps like this:
+
Then we perform 4 compares&swaps like this (pass 2):
  
 
[[File:BitonicBuild1.png]]
 
[[File:BitonicBuild1.png]]
  
Finally we perform 4 compares&swaps like this:
+
Finally we perform 4 compares&swaps like this (pass 3):
  
 
[[File:BitonicBuild2.png]]
 
[[File:BitonicBuild2.png]]
Line 35: Line 35:
 
Well it's another log2(N) algorithm as in part 1. You'll need to swap numbers log2(N) times.
 
Well it's another log2(N) algorithm as in part 1. You'll need to swap numbers log2(N) times.
  
For 8 values, we first perform 4 compares&swaps like this:
+
For 8 values, we first perform 4 compares&swaps like this (pass 1):
  
 
[[File:BitonicCompare0.png]]
 
[[File:BitonicCompare0.png]]
  
Then we perform 4 compares&swaps like this:
+
Then we perform 4 compares&swaps like this (pass 2):
  
 
[[File:BitonicCompare1.png]]
 
[[File:BitonicCompare1.png]]
  
Finally we perform 4 compares&swaps like this:
+
Finally we perform 4 compares&swaps like this (pass 3):
  
 
[[File:BitonicCompare2.png]]
 
[[File:BitonicCompare2.png]]
Line 52: Line 52:
  
 
==Example==
 
==Example==
 +
 +
Here x0, x1, ..., xn}+ describes a set of values in ascending order and {x0, x1, ..., xn}- describes a set of values in descending order. While { {...}+, {...}- }B describes a bitonic sequence.
 +
 +
 +
Initial random sequence: 35, 16, 31, 4, 4, 16, 17, 12
 +
 +
After part 1 pass 1: {{16, 35}+, {31, 4}-}B, {{4, 16}+, {17, 12}-}B  ← We already have 2 size 4 bitonic sequences
 +
 +
After part 1 pass 2: 16, 4, 31, 35, 17, 16, 4, 12  ← It's becoming somewhat random again while we're sorting the 2 size 4 bitonic sequences into a single size 8 sequence
 +
 +
After part 1 pass 3: {{4, 16, 31, 35}+, {17, 16, 12, 4}-}B ← And we're done!

Revision as of 15:17, 11 September 2015

I wanted to write a short explanation about bitonic sort because it's simple as hell but no one does a good job at explaining it correctly.

You can find the video explanation here https://www.youtube.com/watch?v=GEQ8y26blEY but it's a bit tedious so to sum up the algorithm, here's what you need to know:

  • A bitonic sequence is a sequence of numbers that can be ordered as first ascending then descending (or the opposite)
    • For example: (1, 2, 3, 4) followed by (4,3,2,1) is bitonic.
  • The bitonic sort algorithm works in 2 parts:
    • (1) First, you need to transform any random sequence of numbers into a bitonic sequence
    • (2) Second, you can then sort the bitonic sequence in ascending order


And that is all there is to it! Now let's see how to execute the 2 parts of the algorithm.

Quick Algorithm

Part 1: Transforming any sequence of numbers into a bitonic sequence

It's a log2(N) algorithm where we recursively compare and swap elements 2 by 2 in a very specific manner.

For 8 values, we first perform 4 compare&swaps like this (pass 1):

BitonicBuild0.png

Then we perform 4 compares&swaps like this (pass 2):

BitonicBuild1.png

Finally we perform 4 compares&swaps like this (pass 3):

BitonicBuild2.png


Part 2: Sorting the bitonic sequence

Well it's another log2(N) algorithm as in part 1. You'll need to swap numbers log2(N) times.

For 8 values, we first perform 4 compares&swaps like this (pass 1):

BitonicCompare0.png

Then we perform 4 compares&swaps like this (pass 2):

BitonicCompare1.png

Finally we perform 4 compares&swaps like this (pass 3):

BitonicCompare2.png


The way I see it

Example

Here x0, x1, ..., xn}+ describes a set of values in ascending order and {x0, x1, ..., xn}- describes a set of values in descending order. While { {...}+, {...}- }B describes a bitonic sequence.


Initial random sequence: 35, 16, 31, 4, 4, 16, 17, 12

After part 1 pass 1: {{16, 35}+, {31, 4}-}B, {{4, 16}+, {17, 12}-}B ← We already have 2 size 4 bitonic sequences

After part 1 pass 2: 16, 4, 31, 35, 17, 16, 4, 12 ← It's becoming somewhat random again while we're sorting the 2 size 4 bitonic sequences into a single size 8 sequence

After part 1 pass 3: {{4, 16, 31, 35}+, {17, 16, 12, 4}-}B ← And we're done!