This program illustrates sorting by selection. Here when we perform a search through the table, starting from the first record, to locate the element with the smallest key. When this record is found, we interchange it with the first record in the table. As a result of this interchange the smallest key is placed in the first position of the table.
|
select (int a[], int n)
{
int i, j, temp, minind;
i = 0;
while( i < n � 1)
{
/* minind denotes the position of smallest element */
/* found thus, far for a particular iteration */
minind = i ;
j = i + 1;
while (j < n � 1)
{
if (a[j] < a[minind])
minind = j;
j ++;
} /*end of inner loop*/
if (minind ! = i )
{
temp=a[i];
a[i] = a [minind];
a[minind] = temp;
}
i ++;
}/*end of outer loop*/
}/*end of routine */
|