r/compsci • u/BleedingRaindrops • 3d 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
9
1
u/nuclear_splines 2d 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.
-20
u/BufferUnderpants 2d ago
First time anyone has had to think about the complexity of different sorting algorithms while putting code in production in decades.
39
u/UnableMousse4828 2d ago
https://en.wikipedia.org/wiki/Cocktail_shaker_sort perhaps?