JS Loops Help (Variable assignment query)

JS Loops Help (Variable assignment query)
0

#1

Hello, I have a general question (unrelated to any particular challenge) in regard to loops and assigning containers for certain processes related to an algorithm. This is the big thing I’m still struggling with in regard to loops and probably JS in general. Below I have an example of my query. I’ll write the setup for the code and the code itself. I’ve put my queries within it. (The problem is from a book entitled “A Common Sense Guide to Data Structures and Algorithms”

Sort the array in order from least to greatest

var array =[4,2,7,1,3]

function selectionSort(array) {
for(var(i=0;i<array.length;i++){
     var lowestNumberIndex = i;
//this is assigned to i, but why would I think that i would automatically point to the index with the lowest number?  I iterates through all of them and every number would be less than the total length of the array, correct?
    for(var j = i + 1;j < array.length; j++) {
        if(array[j] < array[lowestNumberIndex]) {
            lowestNumberIndex = j;
       }
//One why does assigning i+1 to var j make a difference i determining the index of the lowest number? var i and var j are just variables iterating through an array of numbers.  Each single number will be less than the length of the entire array of numbers, correct?
   }
   if(lowestNumberIndex !== i) {
      var temp = array[i];
      array[i] = array[lowestNumberIndex];
      array[lowestNumberIndex] = temp;
   }

//If the idea is to swap the lowest number for the number at the position the lowest number should be at, then I don't understand how switching the assignments does this.(1) and I don't understand how var i and var lowestNumberIndex have their particular valence in the function when they seem arbitrary parts of the loop (They aren't methods like .minMax or something like this that actually find the lowest element in an array.)
  }
  return array

}

I’m really looking for someone to explain to me how things are working when they seem to me to be arbitrary assignments.

Thank you for the patience of anyone willing to explain this to me.


#2

So for [4,2,7,1,3]

Enter outer loop. i goes from index 0 to 4.

First iteration

i is 0
lowestNumberIndex is 0.

Enter inner loop. j goes from index 1 to 4:
Checks if each array[j] in turn is is less than array[0]. If that’s true, lowestNumberIndex gets reassigned.

2 < 4, lowestNumberIndex is now 1
7 > 2, lowestNumberIndex is still 1
1 < 2, lowestNumberIndex is now 3
3 > 1, lowestNumberIndex is still 3

Inner loop ends, onto reassignment. This is the standard way of swapping variables (take first, store value in a temp var, replace first with second, replace second with temp)

Remember i is 0.
lowestNumberIndex is 3
They are not equal, so swap value at index 3 with the one at index 0.

Smallest value is at index 0, array is [1,2,7,4,3]

Second Iteration

i is 1
lowestNumberIndex is 1

Enter inner loop. j goes from index 2 to 4:
Checks if each array[j] in turn is is less than array[1]. If that’s true, lowestNumberIndex gets reassigned.

7 > 2, lowestNumberIndex is still 1
4 > 2, lowestNumberIndex is still 1
3 > 2, lowestNumberIndex is still 1

Inner loop ends, onto reassignment.

Remember i is 1.
lowestNumberIndex is 1
They are equal, no swapsies.

Smallest value is at index 0, next smallest index 1, array is still [1,2,7,4,3]

Third Iteration

i is 2
lowestNumberIndex is 2

Enter inner loop. j goes from index 3 to 4:
Checks if each array[j] in turn is is less than array[2]. If that’s true, lowestNumberIndex gets reassigned.

4 < 7, lowestNumberIndex is now 3
3 < 4, lowestNumberIndex is now 4

Inner loop ends, onto reassignment.

Remember i is 2.
lowestNumberIndex is 4
They are not equal, so swap value at index 4 with the one at index 2. Smallest value is at index 0, then 1, then 2, array is now [1,2,3,4,7]

Fourth Iteration

i is 3
lowestNumberIndex is 3

Enter inner loop. j goes from index 4 to 4:
Checks if each array[j] in turn is is less than array[3]. If that’s true, lowestNumberIndex gets reassigned.

7 > 4, lowestNumberIndex is still 3.

Inner loop ends, onto reassignment.

Remember i is 3.
lowestNumberIndex is 3
They are equal, no swapsies. Smallest value is at index 0, then 1, then 2, then 3, then 4 (the outer loop will run one more time but thats irrelevant as nothing in it does anything. Array is [1,2,3,4,7], done

Not sure, as there’s no way it can do that.

They’re iterating through the indices of an array, need to do that to actually be able to check the values in the array. It needs to be i +1 because you need to compare two values, cannot sort otherwise.

They are swapping positions of elements in the array, this is the core thing the sort has to do.

This does find the lowest value though