In the selection sort algorithm why do we need to initialize the value of min_index variable with i.
for(int i = 0; i < n -1 ; ++i)
{
int min_index;
for(int j = i; j < n; ++j)
{
if(a[j] < a[i])
min_index = j;
}
swap(a[i], a[min_index]);
}
Here inside the jth loop I am comparing a[j] with a[i].
Now if I just initize the min_index variable it gives me weong output.
Can you explain me why we need to initialixe value of min_index with i although we are replacing with j in the second loop.
Please Help!!!