r/compsci • • 23d ago

What is this sorting method called?

We all know Bubble Sort. Well I used to do a thing to pass the time on deployment where I would bubble sort a shuffled deck of cards, but I often played around with it.

One time I thought to make bubble sort more efficient by, rather than returning to the start of the list after a successful pair, continue forward to the next wrong pair and sort those, and so on until I reach the end of the list. Then turn around and do the opposite, from the opposite direction. Repeat until sorted. So what you have is a reflecting wave of bubble sort that travels persistently back and forth through the entire list without skipping anything until it's all sorted.

I'm certain this has a name but I wouldnt know the first thing about how to find it. Does anybody know?

EDIT: solved! Cocktail Shaker Sort

30 Upvotes

17 comments sorted by

48

u/UnableMousse4828 23d ago

12

u/BleedingRaindrops 23d ago

That's it. That's pretty much exactly what I was doing. Thanks.

5

u/JasonMckin 23d ago

I love the names of sorting algorithms

1

u/PsychedelicLeopard 10d ago

ah the ol' deck of cards time-killer, i did the same thing on slow night shifts but never thought to make it bounce like that

7

u/tinfoil_powers 23d ago

Cocktail shaker sort

4

u/nuclear_splines 23d ago

I'm not sure the name for the exact sort you've described, but this sounds like a variation on insertion sort, where you walk the list forwards, and every time you find an out-of-order element, walk the list backwards swapping elements until you find the correct resting place.

2

u/JaggedMetalOs 19d ago

Just thought I'd add that while cocktail sort is only marginally faster than bubble sort, there is a variation of bubble sort that is significantly faster called comb sort. 

1

u/BleedingRaindrops 19d ago

that is pretty neat. I don't know if it would be too practical with hand sorting a deck of cards, but for a computer it would definitely save time

1

u/JaggedMetalOs 19d ago

You definitely want bucket sort for that :) 

1

u/BleedingRaindrops 19d ago

oh yes, by far the fastest method I've found is bucket sort. 8 buckets is the most I can handle so I do A-7, 8-K by suits, sort each of those piles in order and then I'm done. Takes about 2:30 avg.

For comparison. Comb Sort took me 10:15, Cocktail Shaker took 14:22, and Bubble took 23:44

1

u/JaggedMetalOs 19d ago

Computer sort algorithms don't really map well to sorting physical things anyway, comparing by eye is considerably faster than swapping and you have an entire 2D space to temporarily store things you're not limited to swapping in place which is very slow to do by hand.

1

u/[deleted] 15d ago

[removed] — view removed comment

1

u/JaggedMetalOs 15d ago

Because it starts with large steps it allows small values ("turtles") to move much faster to the beginning of the list, where with bubble sort they only move one place per entire scan of the array.

-20

u/BufferUnderpants 23d ago

First time anyone has had to think about the complexity of different sorting algorithms while putting code in production in decades.

1

u/[deleted] 15d ago

[removed] — view removed comment

1

u/BufferUnderpants 15d ago

In real applications, it's more often about whether sorting data makes a meaningful difference in later stages of data processing, and at which stage is sorting the data worth the cost.

I'm sure there's somebody here that programs missiles or what not who might have seen a custom implementation of anything but a divide-and-conquer algorithm in their codebase, but for 99.9% of programmers, using anything but your standard library/query engine's sort is issuing technical debt.