previous | index | next

Selection Sort Inner Loop

The inner loop performs the scan for the index with the largest element within the part of the array determined by n in the outer loop.

So it will use i for a loop control variable and start it at 1, incrementing at each step:

    for (int n = SIZE-1; n > 0; n--) {
      indexOfLargest = 0;
        for (int i = 1; i <= n; i++) {
           ...                       // inner loop determines indexOfLargest
        }        
      temp = v[n];               // swap the highest position of the unsorted
      v[n] = v[indexOfLargest];  // part with the largest element
      v[indexOfLargest] = temp;
    }

previous | index | next