![]() |
VOOZH | about |
The Selection Sort algorithm sorts maintain two parts.
The algorithm works by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the end of the sorted part.
arr[] = 64 25 12 22 11 // Find the minimum element in arr[0...4] // and place it at beginning 11 25 12 22 64 // Find the minimum element in arr[1...4] // and place it at beginning of arr[1...4] 11 12 25 22 64 // Find the minimum element in arr[2...4] // and place it at beginning of arr[2...4] 11 12 22 25 64 // Find the minimum element in arr[3...4] // and place it at beginning of arr[3...4] 11 12 22 25 64
We have already discussed Iterative Selection Sort. In this article recursive approach is discussed. The idea of a recursive solution is to one by one increment sorted part and recursively call for the remaining (yet to be sorted) part.
Output:
0 1 2 3 5 7
Time Complexity: O(n2)
Auxiliary Space: O(n)