Selection Sort Algorithm

Sample C source code for the selection sort.

/* Sorts by selecting smallest element in unsorted
   portion of array and exchanging it with element
   at the beginning of the unsorted list.
*/
 
void selectionSort(int list[], int n)
{
   int cur, j, smallest, tempData;
   for (cur = 0; cur <= n; cur ++){
          smallest = cur;
          for ( j = cur +1; j <= n ; j++)
              if(list[ j ] < list[smallest])
                 smallest = j ;
 
      // Smallest selected; swap with current element
      tempData = list[cur];
      list[cur] = list[smallest];
      list[smallest] = tempData;
   }
}
Creative Commons License Valid CSS Valid XHTML 1.0 Driven by DokuWiki Powered by PHP Powered by Apache get firefox!! kgareth.com Recent changes RSS feed