1.
const int nSize=6;
int anArray[nSize]={ 30, 60, 20, 50, 40, 10 };
for(int nStartIndex=0;nStartIndex<nSize;nStartIndex++)
{
int nSmallestIndex=nStartIndex;
for(int nCurrentIndex=nStartIndex+1;nCurrentIndex<nSize;nCurrentIndex++)
{
if(anArray[nCurrentIndex]<anArray[nStartIndex])
nSmallestIndex=nCurrentIndex;
}
swap(anArray[nStartIndex],anArray[nSmallestIndex]);
}
2.
const int nSize=6;
int anArray[nSize]={ 30, 60, 20, 50, 40, 10 };
for(int nStartIndex=0;nStartIndex<nSize;nStartIndex++)
{
int nSmallestIndex=nStartIndex;
for(int nCurrentIndex=nStartIndex+1;nCurrentIndex<nSize;nCurrentIndex++)
{
if(anArray[nCurrentIndex]<anArray[nSmallestIndex])
nSmallestIndex=nCurrentIndex;
}
swap(anArray[nStartIndex],anArray[nSmallestIndex]);
}
why do they give different results although nSmallestIndex equals to nStartIndex?
first code results {10,30,20,40,50,60}
second code results {10,20,30,40,50,60}