# logic fail with selection search (and nested for loops in general?)

I hate having to ask this q, but I have to know. In my outer for loop, I made the condition i < n - 1 because arrays are 0 indexed and therefore always one less than the size of the array. I don't understand why the condition changes for the inner loop because the size of the array doesn't change. Using printf, it was easy to see the last element in the array wasn't getting checked and changing j < n - 1 to j < n fixed it, but I don't understand why and am afraid it's going to come back to bite me.

So this code where j < n - 1 misses the last element in the array:

``````// iterates thru array
for (int i = 0; i < n - 1; i++)
{
// makes i current minimum
int min = i;

// iterates thru unsorted part of array
for (int j = i + 1; j < n - 1; j++)
{
// checks if value at j < min value
if (array[j] < array[min])
{
// assigns j as new minimum
min = j;
}
``````

But it works perfectly here when j < n:

``````for (int i = 0; i < n; i++)
{
printf("%i", array[i]);
}

printf("\n");

// iterates thru array
for (int i = 0; i < n - 1; i++)
{
// makes i current minimum
int min = i;

// iterates thru unsorted part of array
for (int j = i + 1; j < n - 1; j++)
{
// checks if value at j < min value
if (array[j] < array[min])
{
// assigns j as new minimum
min = j;
}
``````

Thanks in advance for the help.

First of all, both code blocks are the same. I'll assume you forgot to make the change to `j < n` after cutting and pasting the block.

You're right about an array starting at 0, but it looks like you might have overlooked the impact of "<" vs. "<=".

Let's assume that you're sorting an array of n = 5 elements and let's look at the case of j < n. The i loop is going to compare the i'th element to every element that follows it, so in this case, i needs to run from 0 to 3. (the 4th element is the end of the list.) That part is fine.

Now, the code needs to compare the i'th element to the j'th elements from i+1 to the last element. That means that j needs to run to n-1, or 4, as you clearly understood. There are several ways to get there. Either of these would work: `j < n` or `j <= n-1`. In this example, they would calculate out as `j < 5` and `j <= 4` respectively.

Does that make sense now?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Sorry, yes. I had trouble embedding my code and didn't re-edit it the 2x around. Thanks for seeing through that. – Lindsey Apr 28 '17 at 18:14
• From stepping thru the code w/gbd over and over, I recalled the concept I forget: your answer is the same as saying the condition for a while loop has to fail before the loop ceases to run, isn't it? So, using your example, in an array of 5 elements where the index w/the highest value is 4 due to 0 indexing, if the condition of the inner loop is set to j < n - 1, j can only reach 3 before the condition fails and in order to capture the 4th index (5th element), the fix is to make the condition j <= 4 or j < n. – Lindsey Apr 28 '17 at 18:14