Cocktail Shaker Sort vs Bubble Sort
Same 110 values, same shuffle, same speed. Cocktail Shaker Sort finishes first and turns gold: 7,080 operations to 8,678.
The numbers
- Cocktail Shaker operations
- 7,080
- Bubble operations
- 8,678
- Cocktail Shaker average
- n²
- Bubble average
- n²
- Cocktail Shaker worst
- n²
- Bubble worst
- n²
Cocktail Shaker Sort
Cocktail Shaker Sort is Bubble Sort that runs forwards and then backwards, so small values move quickly too. More on Cocktail Shaker Sort.
Bubble Sort
Bubble Sort walks through the list swapping neighbours that are out of order, until a pass makes no swaps. More on Bubble Sort.
FAQ
Which is faster, Cocktail Shaker Sort or Bubble Sort?
On this shuffle of 110 values, Cocktail Shaker Sort needed 7,080 operations and Bubble Sort needed 8,678, about 1.2× as many. On average Cocktail Shaker Sort is n² and Bubble Sort is n².
Which one is stable?
Both are stable.
How is the race fair?
Both algorithms start from the same shuffle and run the same number of operations per frame. Every comparison, swap and write counts as one operation, so the one that needs fewer operations finishes first.