by Arainach · 1w ago · 0 comments
One important thing to note is that bubble sort places one element in the correct position each pass and then *stops looking at that position ever again*.
This algorithm looks at all N elements every pass.
Others have shared a bunch of great videos and writeups. Without reading them, my initial assumption is that this builds things bottom up (such that after the first pass, the element in position 0 is guaranteed to be the lowest element) and that despite it being compared again and again, it's essentially placing one element each pass, but I could be wrong.
Bubblesort really is a family of "this is something I figured out myself that runs by looping in O(N^2) by moving things to the front/back"
Read it again; First pass ensures that the largest element ends up in position 0. Yet it somehow manages to sort ascending by the end.
It's not very intuitive and it's probably the least efficient sorting algorithm I've seen besides sleepsort[1]
[1]: https://www.reddit.com/r/programminghorror/comments/lgsd18/i...