The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
Following example explains the above steps:
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
Flowchart of the Selection Sort:
Output:
Sorted array:
11 12 22 25 64
Time Complexity: O(n2) as there are two nested loops.
Auxiliary Space: O(1)
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
Stability: The default implementation is not stable. However it can be made stable. Please see stable selection sort for details.
In Place: Yes, it does not require extra space.
Snapshots:
Quiz on Selection Sort
Other Sorting Algorithms on GeeksforGeeks/GeeksQuiz:
81 videos|80 docs|33 tests
|
1. What is selection sort and how does it work? |
2. What is the time complexity of selection sort? |
3. Is selection sort stable? |
4. When is selection sort a good choice for sorting? |
5. Can selection sort be used for sorting in descending order? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|