i need help with this JS sort algorithm


#1

So, it works perfectly. i just dont understand it. anyone willing to comment the step by step or give me an easy algorithm

var numArray = [25,4,9,41, 7,9,11,0];

for (var i = 0; i < numArray.length - 1; i++) {
var min = i;
for (var j = i + 1; j < numArray.length; j++) {
if(numArray[min]> numArray[j]){min=j;}
}
if(min!=i){
var target= numArray[i];
numArray[i]=numArray[min];
numArray[min] = target;
}
}
//console.log(numArray);


#2

This is a very basic sort that goes through two levels of looping. The outer loop simply determines what number you are sorting. The second loop compares the item you are trying to sort with every other item to see if there are any smaller items. It is similar to what I describe as part of this: https://www.kirupa.com/sorts/insertionsort.htm :slight_smile: