~ Office Supplies ~~ Buy Posters ~~ A-Z Products ~~ Website Advertising


Selection sort - Wikipedia

<<Up     Contents

Selection sort

Selection sort is a sort algorithm that works as follows:
remove the lowest datum one at a time until the set of data is empty

The naive algorithm, iterating through a list of n unsorted items, has a worst-case, average-case and best-case run-time of O(n2), assuming that comparisons can be done in constant time.

Heapsort optimizes the algorithm by using a heap data structure to speed up the finding and removing of the lowest datum.

Example

Here is a simple implementation of selection sort in C (from pd lecture notes (http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture1.html)):

int find_min_index (float [], int, int);
void swap (float [], int, int);
 
/* selection sort on array v of n floats */
 
void selection_sort (float v[], int n) {
  int  i;
 
  /* for i from 0 to n-1, swap v[i] with the minimum
   * of the i'th to the n'th array elements
   */
  for (i=0; i<n-1; i++) 
    swap (v, i, find_min_index (v, i, n));
} 
 
/* find the index of the minimum element of float array v from
 * indices start to end
 */
int find_min_index (float v[], int start, int end) {
  int  i, mini;
 
  mini = start;
  for (i=start+1; i<end; i++) 
    if (v[i] < v[mini]) mini = i;
  return mini;
}
 
/* swap i'th with j'th elements of float array v */
void swap (float v[], int i, int j) {
  float	t;
 
  t = v[i];
  v[i] = v[j];
  v[j] = t;
}

wikipedia.org dumped 2003-03-17 with terodump




 
 
13 ct pink red gemmy banded RHODOCHROSITE Gorgeous gemstone tongue freeform 14 mm Single gem piece
 13 ct pink red my banded RHODOCHROSITE Gorgeous tongue freeform 14 mm Single piece 
 
7 carat RARE yellow orange CLINOHUMITE gem stone crystal cabbing lapidary cabochon rough 1 gram C
 7 carat RARE yellow orange CLINOHUMITE crystal cabbing lapidary cabochon 1 gram C 
 
14 gram red blue gold PIETERSITE gem stone Tumbled cab cabbing rough raw gemstone 73 carat PRETTY
 14 gram red blue gold PIETERSITE Tumbled cab cabbing raw 73 carat PRETTY 
 
50 cts Neon Blue Green APATITES gem stones Jewelry rough tumbled polished gemstones lots 10 grams
 50 cts Neon Blue Green APATITES Jewelry tumbled polished lots 10 grams 
 
25 carats CHRYSOBERYL gems stones Facet uncut raw rough gemstones crystals lot 1 to 2 ct jewels Nice
 25 carats CHRYSOBERYL uncut raw crystals lot 1 to 2 ct jewels Nice