ENCYCLOPEDIA 4U .com



Encyclopedia Home Page

Google
  Web Encyclopedia4u.com

 

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):

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} 
/* 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}
/* 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;
}




Content on this web site is provided for informational purposes only. We accept no responsibility for any loss, injury or inconvenience sustained by any person resulting from information published on this site. We encourage you to verify any critical information with the relevant authorities.



Copyright © 2005 Par Web Solutions All Rights reserved.
| Privacy

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Selection sort".