FACTOID # 106: Americans are 15% more innovative than the Japanese. But in percentage terms, the Japanese grant 3.5 times more patents.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Selection sort

Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has Θ(n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. It works as follows: In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. ... In computer science, an in-place algorithm is an algorithm which transforms a data structure using a small, constant amount of extra storage space. ... A comparison sort is a particular type of sorting algorithm; a number of well-known algorithms are comparison sorts. ... Big O notation is often used to describe how the size of the input data affects an algorithms running time. ... Example of insertion sort sorting a list of random numbers. ...

  1. Find the minimum value in the list
  2. Swap it with the value in the first position
  3. Repeat the steps above for remainder of the list (starting at the second position)


Effectively, we divide the list into two parts: the sublist of items already sorted, which we build up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array.


Here is an example of this sort algorithm sorting five elements:

 31 25 12 22 11 11 25 12 22 31 11 12 25 22 31 11 12 22 25 31 

Selection sort can also be used on list structures that make add and remove efficient, such as a linked list. In this case it's more common to remove the minimum element from the remainder of the list, and then insert it at the end of the values sorted so far. For example: In computer science, a linked list is one of the fundamental data structures. ...

 31 25 12 22 11 11 31 25 12 22 11 12 31 25 22 11 12 22 31 25 11 12 22 25 31 

Implementation

The following is a C/C++ implementation, which makes use of a swap function: It has been suggested that this article or section be merged with XOR swap algorithm. ...

 void selectionSort(int a[], int size) { int i, j, min; for (i = 0; i < size - 1; i++) { min = i; for (j = i+1; j < size; j++) { if (a[j] < a[min]) { min = j; } } swap(a[i], a[min]); } } 

Python example:

 def selection_sort(A): for i in range(0, len(A)-1): min = A[i] pos = i for j in range(i+1, len(A)): if( A[j] < min ): min = A[j] pos = j A[pos] = A[i] A[i] = min 

Analysis

Selection sort is not difficult to analyze compared to other sorting algorithms since none of the loops depend on the data in the array. Selecting the lowest element requires scanning all n elements (this takes n - 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n - 1 elements and so on, for (n - 1) + (n - 2) + ... + 2 + 1 = n(n - 1) / 2 = Θ(n2) comparisons (see arithmetic progression). Each of these scans requires one swap for n - 1 elements (the final element is already in place). Thus, the comparisons dominate the running time, which is Θ(n2). In mathematics, an arithmetic progression or arithmetic sequence is a sequence of numbers such that the difference of any two successive members of the sequence is a constant. ...


Comparison to other Sorting Algorithms

Among simple average-case Θ(n2) algorithms, selection sort always outperforms bubble sort and gnome sort, but is generally outperformed by insertion sort. Insertion sort is very similar in that after the kth iteration, the first k elements in the array are in sorted order. Insertion sort's advantage is that it only scans as many elements as it needs to in order to place the k + 1st element, while selection sort must scan all remaining elements to find the k + 1st element. Bubble sort is a simple sorting algorithm. ... Gnome sort is a sort algorithm which is similar to insertion sort except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. ... Example of insertion sort sorting a list of random numbers. ...


Simple calculation shows that insertion sort will therefore usually perform about half as many comparisons as selection sort, although it can perform just as many or far fewer depending on the order the array was in prior to sorting. It can be seen as an advantage for some real-time applications that selection sort will perform identically regardless of the order of the array, while insertion sort's running time can vary considerably. However, this is more often an advantage for insertion sort in that it runs much more efficiently if the array is already sorted or "close to sorted." Realtime redirects here. ...


Another key difference is that selection sort always performs Θ(n) swaps, while insertion sort performs Θ(n2) swaps in the average and worst cases. Because swaps require writing to the array, selection sort is preferable if writing to memory is significantly more expensive than reading, such as when dealing with an array stored in EEPROM or Flash. An EEPROM (also called an E2PROM)[] or Electronically Erasable Programmable Read-Only Memory, is a non-volatile storage chip used in computers and other devices to store small amounts of volatile (configuration) data. ... A USB flash drive. ...


Finally, selection sort is greatly outperformed on larger arrays by Θ(nlog n) divide-and-conquer algorithms such as quicksort and mergesort. However, insertion sort or selection sort are both typically faster for small arrays (ie less than 10-20 elements). A useful optimization in practice for the recursive algorithms is to switch to insertion sort or selection sort for "small enough" sublists. In computer science, divide and conquer (D&C) is an important algorithm design paradigm. ... Quicksort in action on a list of random numbers. ... In computer science, merge sort or mergesort is a sort algorithm for rearranging lists (or any other data structure that can only be accessed sequentially, e. ...


Improving Selection Sort With Boolean

We can improve selection sort with using boolean. we can stop the iteration if data has been sorted. example in PASCAL :

 { clx321 } procedure OpSelectionSort (tabel : DataArray, N : integer); {initial : tabel has been filled} {final : tabel has been sorted} var sorted : boolean; pass : integer; i : integer; Imin : integer; Temp : integer; begin sorted := false; pass := 1; while ((pass < N) AND (NOT sorted)) do begin Imin := pass; sorted := true; for i := pass+1 to N do begin if ( Tabel [Imin] > Tabel [i] ) then begin Imin := i; sorted := false; end; end; temp := Tabel [pass]; Tabel [pass] := Tabel [Imin]; Tabel [Imin] := temp; pass := pass+1; end; end; 

Variants

Heapsort greatly improves the basic algorithm by using an implicit heap data structure to speed up finding and removing the lowest datum. If implemented correctly, the heap will allow finding the next lowest element in Θ(log n) time instead of Θ(n) for the inner loop in normal selection sort, reducing the total running time to Θ(n  log n). A run of the heapsort algorithm sorting an array of randomly permuted values. ... Implicit data structure is a data structure that uses very little memory besides the actual data elements. ... Example of a complete binary max heap In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) ≥ key(B). ... A binary tree, a simple type of branching linked data structure. ...


A bidirectional variant of selection sort, called cocktail sort, is an algorithm which finds both the minimum and maximum values in the list in every pass. This reduces the number of scans of the list by a factor of 2, eliminating some loop overhead but not actually decreasing the number of comparisons or swaps. Note, however, that cocktail sort more often refers to a bidirectional variant of bubble sort. Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort, ripple sort, shuttle sort or happy hour sort, is a stable sorting algorithm that varies from bubble sort in that instead of repeatedly passing through the list from top to bottom, it passes alternately from top to...


Selection sort can be implemented as a stable sort. If, rather than swapping in step 2, the minimum value is inserted into the first position (that is, all intervening items moved down), the algorithm is stable. However, this modification leads to Θ(n2 ) writes, eliminating the main advantage of selection sort over insertion sort, which is always stable. In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. ...


References

  • Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 138–141 of Section 5.2.3: Sorting by Selection.

Donald Ervin Knuth ( or Ka-NOOTH[1], Chinese: [2]) (b. ...

External links


  Results from FactBites:
 
Selection Sort (1463 words)
Selection sort is probably the simplest sorting algorithm to implement so it can be blazingly fast on short arrays.
The idea of selection sort is rather simple: we repeatedly find the next largest (or smallest) element in the array and move it to its final position in the sorted array.
The implementation of the selection sort algorithm in C, together with a driver program is shown in Figure 10.6.
Cprogramming.com - Algorithms - Insertion Sort and Selection Sort (327 words)
Selection sort is the most conceptually simple of all the sorting algorithms.
It works by selecting the smallest (or largest, if you want to sort from big to small) element of the array and placing it at the head of the array.
Because a selection sort looks at progressively smaller parts of the array each time (as it knows to ignore the front of the array because it is already in order), a selection sort is slightly faster than bubble sort, and can be better than a modified bubble sort.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.