I need to write a program that sorts some data into ascending order. I was looking in my programming book and found a sample program that does that such thing. I am a little confused on some of the aspects of the code they used, and they didn't comment on it very well so i am not sure what's going on. I was wondering if someone could give a few insights into why some things are the way they are so that I can write my own program. Book code is as follows:
void sort(int x[], int npts)
{
int k, j, m ;
double hold ;
for (k=0; k <=npts-2 ; k++)
{
m = k ;
for (j=k+1; j <= npts -1; j++)
if (x[j] < x[m])
m = j;
hold = x[m];
x[m] = x[k];
x[k] = hold;
}
return ;
}
So here are the things I don't have a clue on.
-Why does the first for loop run up to k <=npts-2?
-What is the purpose of the line m = k?
-Why does the second loop start at j = k+1?
-What is the purpose opf the line m = j inside the second loop?
-Why do values get shuffled about in the hold thing?
So basically, I have no idea what is going on here. I figure knowing why this works will help me write the program I have to write...but, I doubt it. Thanks y'all.