4 ms·
That’s just a bubble sort. I learned that in elementary school in the 80s.
by scarface74 5y ago
That’s just a bubble sort. I learned that in elementary school in the 80s.
- cable2600 5y agoBubble sort is slow.
- scarface74 5y agoI realize that. But tweet was acting like it was “One Weird Trick” that was a new discovery.
- st0le 5y agoDid you see the inequality sign?
- cato_the_elder 5y agoNo, it's not. But that's kinda the point. This is mildly interesting because at the first glance it kinda looks like a wrongly implemented bubble sort, but it actually works.
- twisteriffic 5y agoIt's a slightly modified selection sort. Bubble compares arr[j] to arr[j+1]. Selection sort normally compares arr[j] to arr[indexofsmallest]. The algorithm here avoids the need to track smallest index by just swapping the next smallest value found to a known location, arr[i]. Trades a single pointer worth of memory for n^2 swaps.
- cato_the_elder 5y agoI think it's more similar to insertion sort. In the first iteration of the outer loop, it moves the maximum to A[1]. After that, it works essentially like insertion sort, except that it does a bunch of unnecessary comparisons after finding the location to insert the element, and does the shifting in a slightly weird way.