
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort


Task

Sort an array (or list) of elements using the Selection sort algorithm.



It works as follows:

First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.



Its asymptotic complexity is    O(n2)    making it inefficient on large arrays.

Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM.

No other sorting algorithm has less data movement.



References

   Rosetta Code:   O     (complexity).
   Wikipedia:   Selection sort.
   Wikipedia:   [Big O notation].


 
class
	SELECTION_SORT [G -> COMPARABLE]
 
feature {NONE}
 
	index_of_min (ar: ARRAY [G]; lower: INTEGER): INTEGER
			--Index of smallest element in 'ar' in the range of lower and the max index.
		require
			lower_positiv: lower >= 1
			lower_in_range: lower <= ar.count
			ar_not_void: ar /= Void
		local
			i: INTEGER
			min: G
		do
			from
				i := lower
				min := ar.item (i)
				Result := i
			until
				i + 1 > ar.count
			loop
				if ar.item (i + 1) < min then
					min := ar.item (i + 1)
					Result := i + 1
				end
				i := i + 1
			end
		ensure
			result_is_set: Result /= Void
		end
 
	sort (ar: ARRAY [G]): ARRAY [G]
			-- sort array ar with selectionsort
		require
			ar_not_void: ar /= Void
		local
			min_index: INTEGER
			ith: G
		do
			create Result.make_empty
			Result.deep_copy (ar)
			across
				Result as ic
			loop
				min_index := index_of_min (Result, ic.cursor_index)
				ith := Result [ic.cursor_index]
				Result [ic.cursor_index] := Result [min_index]
				Result [min_index] := ith
			end
		ensure
			Result_is_set: Result /= Void
			Result_sorted: is_sorted (Result) = True
		end
 
	is_sorted (ar: ARRAY [G]): BOOLEAN
			--- Is 'ar' sorted in ascending order?
		require
			ar_not_empty: ar.is_empty = False
		local
			i: INTEGER
		do
			Result := True
			from
				i := ar.lower
			until
				i = ar.upper
			loop
				if ar [i] > ar [i + 1] then
					Result := False
				end
				i := i + 1
			end
		end
 
feature
 
	selectionsort (ar: ARRAY [G]): ARRAY [G]
		do
			Result := sort (ar)
		end
 
end
 