The sorting algorithm is called "Bubble Sort" because smaller (or larger) elements move towards the end of the sequence, just like how bubble rises from the bottom of a lake to the surface.
let's say i have 2,4,3,5,1.
first step, i compare 1 to 5, 1 is less than 5 so we swap: 2,4,3,1,5
then compare 1 to 3, 1 is less than 3 so swap again: 2,4,1,3,5
then 1 to 4, again, swap: 2,1,4,3,5
then 1 to 2, swap: 1,2,4,3,5
see how 1 'bubbles' towards the left since it's smaller than all other elements?
now 1 is confirmed as the smallest element in the sequence.
compare 5 to 3, no swap: 1 --- 2,4,3,5
3 to 4, swap: 1 --- 2,3,4,5
3 to 2, no swap: 1 --- 2,3,4,5
now 2 is confirmed as smallest from what's left thus: 1,2 --- 3,4,5
continue until all elements are confirmed as ordered.
implementing it into the code, the outer loop for ($i ... ) handles the ordered vs unordered elements while the inner loop for ($j ... ) handles the actual comparison and swapping.
Has anyone here ever used Warcraft 3's Worldeditor?
Yes, Warcraft 3 is old news, I get it, but a lot of the timeless gems (DOTA, anyone?) can be made with the powerful tools available in Worldeditor.
In fact, if you want to go beyond coding with the triggers editor, there is even a language for the game, called JASS, though I never bothered to learn that. JASS gives you even more control over the game.
I totally recommend Worldeditor for anyone who likes warcraft, from coding veterans (JASS), to complete newbies (the Trigger Editor is basically child's play).