简体   繁体   中英

Sorting an array with simple algorithm but loop becomes infinite

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.

When you increment i in scope of "j"-cycle, you do not check that i is less than array length. Ie during execution of this cycle you refer to not existed element of array and add it to the array. So you need to change condition in the second for statement:

for(var i = 0; i<numberList.length; i++){
    var reset = i;
    for(var j = 1; j<numberList.length &&  i<numberList.length; j++){

So if we go step-by-step through you code, we can find the next:

i = 0, j = 1: [44, 58, 12, 53, 25, 63]
i = 1, j = 2: [44, 12, 58, 53, 25, 63]
i = 2, j = 3: [44, 12, 53, 58, 25, 63]
i = 3, j = 4: [44, 12, 53, 25, 58, 63]
i = 4, j = 5: [44, 12, 53, 25, 58, 63]

i = 1, j = 1: [44, 12, 53, 25, 58, 63]
i = 2, j = 2: [44, 12, 53, 25, 58, 63]
i = 3, j = 3: [44, 12, 53, 25, 58, 63]
i = 4, j = 4: [44, 12, 53, 25, 58, 63]
i = 5, j = 5: [44, 12, 53, 25, 58, 63]

i = 2, j = 1: [44, 53, 12, 25, 58, 63]
i = 3, j = 2: [44, 53, 25, 12, 58, 63]
i = 4, j = 3: [44, 53, 25, 58, 12, 63]
i = 5, j = 4: [44, 53, 25, 58, 63, 12]
i = 6, j = 5: // i == numberList.length

So that is a reason of yours infinite-loop

try this one

var numberList = [44, 58, 12, 53, 25, 63];  
    var intArrayLength = numberList.length;
    for(var i = 0; i <intArrayLength; i++){

        for(var j = i+1; j < intArrayLength; j++){

            if(numberList[i] > numberList[j] ){
                var swapNumber = numberList[j];
                numberList[j] = numberList[i];
                numberList[i] = swapNumber;
            }                  
        }   

    } 
    document.write(numberList); 

EDIT :

inner for loop can be written in this way also without using temp variable.

for(var j = i+1; j < intArrayLength; j++){

            if(numberList[i] > numberList[j] ){
                numberList[j] += numberList[i];
                numberList[i] =  numberList[j]-numberList[i];
                numberList[j] =  numberList[j]-numberList[i];
            }                  
        }
for(var i = 0; i<numberList.length-1; i++){ 
   for(var j = i+1; j<numberList.length; j++){
        if(numberList[j]<numberList[i]){
            var k = numberList[i];
            var l = numberList[j];
            numberList[i] = l;
            numberList[j] = k;
        }                   
    }     
} 

Every time i is incremented, the value of reset is then set to the new i value.

When you have the line i = reset; it doesn't make any difference, because at the top you have already told it that reset = i . Try taking the reset variable out.

Also, the .length command will give back the number of values in the array. In this case length = 6 . However, the final index of the array is actually number 5 because it starts at 0 (0, 1, 2, 3, 4, 5 = 6 indices). So when i is 5, it is still less than the length and so the code you have written will increment i to 6, which is out of bounds on the array.
To fix this, you could change the code to read...

for(var i = 0; i< (numberList.length-1); i++)

so that when i is 5, it wont be incremented any further because 5 is the last index value of the array.

I hope that helps. (and makes sense :) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM