Select sort is a very simple algorithm but it’s very inefficient for large lists, but it is good when memory is an issue, since it is an in-place sorting algorithm. Meaning it is an algorithm that doesn’t need to use extra memory to store sorted from unsorted elements.
The way it works is the following: Take the fist element in the list and compare with the next elements to find the minimum value of the list. If found swap values. Repeat starting with second element and so on until reaching the last element in the list.
Big-O notation
O(n2)
Pseudo code
for each current_element in the list: assume_minimum = current_element for each element in the list: if assume_minimum > element[value] assume_minimum = element[value] swap current element with assume_minimum values
PHP code example
/** * Select sort * * @param array $list The unsorted list * @return array The sorted list */ function select_sort($list) { for($i = 0; $i <= count($list); $i++) { $min_position = $i; for($j = $i + 1; $j <= count($list); $j++) { if($list[$min_position] > $list[$j]) { $min_position = $j; } } list($list[$i], $list[$min_position]) = array($list[$min_position], $list[$i]); } return $list; }
Python code example
def select_sort(a_list): """ Performs a select sort on list """ for i in range(0,len(a_list)-1): min_position = i for j in range(i+1,len(a_list)): if a_list[min_position] > a_list[j]: min_position = j a_list[i], a_list[min_position] = a_list[min_position], a_list[i] return a_list
One reply on “Select sort algorithm explained with code and dance”
Here is complete description of Heap Sort Algorithm with example and screenshots
http://geeksprogrammings.blogspot.in/2013/08/explain-heapsort-with-example.html
Here is complete description of Heap Sort Algorithm with example and screenshots
http://geeksprogrammings.blogspot.in/2013/08/explain-heapsort-with-example.html