Selection Sort
Overview
Selection sort is a commonly used comparison-based sorting algorithm. It's very simple to implement and works on the premise that two subarrays are maintained: one which is sorted, and one which is unsorted. In each step, one more element of the array gets sorted, until the entire array is sorted.
Algorithm
The concept of selection sort is that each time, we find the minimum element in the unsorted subarray, and we put it at the end of the sorted subarray.
Consider an array with n distinct elements. Looping times, we
- find the minimum element in the unsorted subarray
- move that element to the end of the sorted subarray
The reason the algorithm works is that each time the smallest value in the unsorted array is the largest value in the sorted array.
Example: 0075 - Sort Colors
An array of integers is given, with n values that are either , , or (representing red, white, or blue). We have to sort them in place so that adjacent elements are together.
In order to achieve this, we can use selection sort.
Pseudocode
- We loop through the array once.
- For each iteration, we keep the index of seperation of the sorted and unsorted subarrays.
- Then, we loop through the array, keeping the index of the minimum element.
- Finally, we move that element to the end of the sorted subarray, and increment the index of seperation.
- We keep doing this until we reach the end of the array, and print out the resulting array.
Dry Run
Let's see the algorithm work on the first test case: and .
In the first iteration, we find the minimum value from index to , which is . This value is moved to the -th index.
The array is now .
In the second iteration, we find the minimum value from index 1 to 5, which is 0. This value is moved to the 1st index.
The array is now .
In the third iteration, we find the minimum value from index 2 to 5, which is 1. This value is moved to the 2nd index.
The array is now .
In the fourth iteration, we find the minimum value from index 3 to 5, which is 1. This value is moved to the 3th index.
The array is now .
In the fifth iteration, we find the minimum value from index 4 to 5, which is 2. This value is moved to the 4th index.
The array is still .
In the last iteration, we find the minimum value from index 5 to 5, which is 2. This value is moved to the 5th index.
The array is still .
Now, let's look at the solution to the example question.
- C++
class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
// Loop once through the array, keeping the seperation index of the sorted and unsorted subarray
for (int i = 0; i < n - 1; i++) {
// index of mini element
int midx = i;
// Loop through array again to find the index of the minimum value in the unsorted subarray
for (int j = i + 1; j < n; j++) {
// If the current element is smaller than the current minimum element
if (nums[j] < nums[midx]) {
// Keep the index of the actual minimum element
midx = j;
}
}
// Swap the minimum value with the current array element (the end of the sorted subarray)
swap(nums[i], nums[midx]);
}
}
};
Complexity
The time complexity of this program is , since we run two for loops to iterate through the array.
The space complexity of this program is , since we don't need any extra space other than our original array.
Suggested Problems
Problem Name | Difficulty | Solution Link |
---|---|---|
0075 - Sort Colors | Easy | View Solutions |
0268 - Missing Number | Easy | View Solutions |
0448 - Find All Numbers Disappeared In An Array | Easy | N/A |