I am trying to sort an array elements. I have made a very simple algorithm of mine. Here is it
<html>
<head>
<script>
var numberList = [44, 58, 12, 53, 25, 63];
for(var i = 0; i<numberList.length; i++){
var reset = i;
for(var j = 1; j<numberList.length; j++){
if(numberList[j]<numberList[i]){
var k = numberList[i];
var l = numberList[j];
numberList[i] = l;
numberList[j] = k;
i++;
} else{
var k = numberList[i];
var l = numberList[j];
numberList[i] = k;
numberList[j] = l;
i++;
}
}
i = reset;
}
document.write(numberList);
</script>
</head>
<body>
</body>
</html>
But the problem is loop becomes infinite. Each time after execution of inner for loop, the value of variable i becomes 5. So I have introduced a variable named reset to restore the value of i. So that i is again set to it's primary value & then first for loop make an increment to it. But it becomes infinite. The same algorithm is working fine in another program. But here it's no. I appreciate your help.
Array.prototype.sort()? Why re-invent the wheel for something that's already built into javascript environmentiinside thejloop.