its a standard sorting algorithm. it sorts the elements in the numb[] array in ascending order. it is assumed that the array has 7 elements at addresses 0 to 6.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
for(i = 0; i <= 5; ++i) // loop through the 1st six elements
{
for(j = i + 1; j <= 6; j++) // for each ith element loop through the all the elements after it.
{ // after this loop, smallest element after ith position will be placed at the ith position.
int temp;
if(numb[i] > numb[j]) // swap if ith element is > jth element
{
temp = numb[i];
numb[i] = numb[j];
numb[j] = temp;
}
}
}
|
say initial list numb = [6,5,4,3,2,1,0]
after i=0 loop iteration: numb = [0,6,5,4,3,2,1]
after i=1 iteration: numb = [0,1,6,5,4,3,2]
after i=2 iteration: numb = [0,1,2,6,5,4,3]
after i=3 iteration: numb = [0,1,2,3,6,5,4]
after i=4 iteration: numb = [0,1,2,3,4,6,5]
after i=5 iteration: numb = [0,1,2,3,4,5,6]
hope it clarifies..