@queerlilhayseed@piefed.blahaj.zone
2026-09-01 18:22 UTC
Replies (1)
-
@14th_cylon@lemmy.zip 2026-09-02 03:22
Sort of a “two wrongs that accidentally make a right” maneuver. not exactly (if i understand it correctly). the first swap of a pair, where i < j, basically does not matter, since the same pair will be revisited one more time later with switched values (i = 8, j = 9 does not matter. i = 9, j = 8 does) and that is when the actual sorting happens. that is why the condition is if a[I] < a[j] then swap, which may seem countreintuitive, but we are comparing the values in the reversed order compared to most of the sorting algorithms. the i < j part is the part that is handled in bubble sort by making the inner loop progressively smaller as the array is partially sorted. it is just ignored here, which is obviously bad for any kind of efficiency, but it allows for that super simple code.