Your Progress
53% complete. Continue learning to master DSA!
Selection Sort
Selection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted part of the array and puts it at the beginning of the unsorted part.
Key Takeaways
- Selection sort divides the array into a sorted and an unsorted region
- It has O(n²) time complexity for all cases (best, average, worst)
- Selection sort performs the minimum number of swaps (n-1) compared to other algorithms
- It's an in-place algorithm with O(1) space complexity
- Despite its simplicity, it's inefficient for large datasets but useful for teaching purposes
How Selection Sort Works
Selection sort is a straightforward sorting algorithm that works by repeatedly finding the minimum (or maximum) element from the unsorted portion of the list and moving it to the sorted portion of the list.
Algorithm Steps
- Set the first position as the minimum
- Compare this element with all elements to its right
- If a smaller element is found, mark that position as the new minimum
- After finishing the comparisons, swap the minimum with the first element of the unsorted part
- Move the boundary of the sorted region one element to the right
- Repeat steps 1-5 until the entire array is sorted
Visual Example
Let's sort the array [64, 25, 12, 22, 11] using selection sort:
Initial array: [64, 25, 12, 22, 11] First pass: - Find minimum in [64, 25, 12, 22, 11]: 11 at index 4 - Swap 64 and 11: [11, 25, 12, 22, 64] - Sorted region: [11] | Unsorted region: [25, 12, 22, 64] Second pass: - Find minimum in [25, 12, 22, 64]: 12 at index 2 - Swap 25 and 12: [11, 12, 25, 22, 64] - Sorted region: [11, 12] | Unsorted region: [25, 22, 64] Third pass: - Find minimum in [25, 22, 64]: 22 at index 1 - Swap 25 and 22: [11, 12, 22, 25, 64] - Sorted region: [11, 12, 22] | Unsorted region: [25, 64] Fourth pass: - Find minimum in [25, 64]: 25 at index 0 - No swap needed (already in place) - Sorted region: [11, 12, 22, 25] | Unsorted region: [64] Final pass: - Only one element left in unsorted region, so we're done - Final sorted array: [11, 12, 22, 25, 64]
Implementing Selection Sort
JavaScript Implementation
function selectionSort(arr) { const n = arr.length; // Traverse through all array elements for (let i = 0; i < n - 1; i++) { // Find the minimum element in remaining unsorted array let minIndex = i; for (let j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // Swap the found minimum element with the first element if (minIndex !== i) { // Swap using destructuring assignment [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; } } return arr; } // Example usage const array = [64, 25, 12, 22, 11]; console.log(selectionSort(array)); // Output: [11, 12, 22, 25, 64]
Python Implementation
def selection_sort(arr): n = len(arr) # Traverse through all array elements for i in range(n - 1): # Find the minimum element in remaining unsorted array min_index = i for j in range(i + 1, n): if arr[j] < arr[min_index]: min_index = j # Swap the found minimum element with the first element if min_index != i: arr[i], arr[min_index] = arr[min_index], arr[i] return arr # Example usage array = [64, 25, 12, 22, 11] print(selection_sort(array)) # Output: [11, 12, 22, 25, 64]
C++ Implementation
#include <iostream> #include <vector> std::vector<int> selectionSort(std::vector<int> arr) { int n = arr.size(); // Traverse through all array elements for (int i = 0; i < n - 1; i++) { // Find the minimum element in remaining unsorted array int min_index = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min_index]) { min_index = j; } } // Swap the found minimum element with the first element if (min_index != i) { std::swap(arr[i], arr[min_index]); } } return arr; } int main() { std::vector<int> array = {64, 25, 12, 22, 11}; std::vector<int> sorted = selectionSort(array); // Print the sorted array for (int num : sorted) { std::cout << num << " "; } // Output: 11 12 22 25 64 return 0; }
Time and Space Complexity
Time Complexity
- Best Case: O(n²) - Even if the array is already sorted, the algorithm still needs to check every element to confirm it's sorted
- Average Case: O(n²) - The algorithm performs the same number of comparisons regardless of the input
- Worst Case: O(n²) - When the array is sorted in reverse order, the algorithm still performs the same number of operations
The number of comparisons is always (n(n-1))/2, regardless of the initial order of elements. However, selection sort performs a minimum number of swaps compared to other algorithms like bubble sort.
Space Complexity
- Space Complexity: O(1) - Selection sort is an in-place sorting algorithm that doesn't require any extra space proportional to the input size
- Only a few extra variables are needed regardless of the array size
Advantages and Disadvantages
Advantages
- Simple implementation: The algorithm is straightforward and easy to understand
- Minimum swaps: Selection sort performs the minimum number of swaps (n-1 in the worst case)
- In-place sorting: Doesn't require extra memory beyond a few variables
- Performs well on small arrays: For small datasets, the overhead of more complex algorithms might not be justified
- Performance independent of data: Consistent performance regardless of input data distribution
Disadvantages
- Inefficient for large datasets: O(n²) time complexity doesn't scale well
- Not adaptive: Performance doesn't improve when the array is partially sorted
- Not stable: The relative order of equal elements may change
- Always performs O(n²) comparisons: Even in best-case scenarios
- Outperformed by more advanced algorithms: Algorithms like merge sort or quick sort are much faster for larger arrays
Stability of Selection Sort
A sorting algorithm is said to be stable if it preserves the relative order of equal elements in the sorted output as they appeared in the original input.
Note: Selection sort, as typically implemented, is not stable. This is because it may swap elements that are far apart, potentially changing the relative ordering of equal elements.
Example of Instability
Consider sorting an array of objects with keys and values:
Initial array: [(4,1), (3,2), (4,3), (2,4)] ^ ^ These two elements have the same key (4) After selection sort: [(2,4), (3,2), (4,1), (4,3)] ^ ^ The relative order of these equal elements changed
In the original array, (4,1) appeared before (4,3), but after sorting, the order might be reversed, depending on the implementation.
It is possible to implement a stable version of selection sort, but it typically requires additional space or more complex logic.
Comparison with Other Sorting Algorithms
Algorithm | Best Case | Average Case | Worst Case | Space | Stable |
---|---|---|---|---|---|
Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
Compared to other sorting algorithms, selection sort has the following characteristics:
- Performs fewer swaps than bubble sort, but the same number of comparisons
- Less adaptive than insertion sort, which performs better on partially sorted arrays
- Significantly slower than more advanced algorithms like merge sort and quick sort for large datasets
- Simpler to implement than heap sort, though both are unstable sorting algorithms
Practical Applications
When to Use Selection Sort
- Small datasets: When working with small arrays or lists, the simplicity of selection sort might outweigh the performance benefits of more complex algorithms
- Limited memory: In environments with constrained memory, the in-place nature of selection sort (O(1) space complexity) is advantageous
- Minimizing write operations: Selection sort performs the minimum possible number of swaps (at most n-1), which can be beneficial when write operations are significantly more expensive than read operations
- Educational purposes: Selection sort is often taught in computer science courses due to its simplicity and as a foundation for understanding more complex sorting algorithms
Real-World Scenarios
- Embedded systems with limited memory where an in-place, low-overhead sorting algorithm is needed
- Small data collections where the overhead of more complex algorithms isn't justified
- Systems with expensive write operations (such as certain types of memory) where minimizing the number of swaps is critical
- Educational settings for teaching the fundamentals of sorting algorithms
Variations of Selection Sort
1. Bidirectional Selection Sort (Cocktail Selection Sort)
This variation alternates between finding the minimum and maximum elements, placing them at the beginning and end of the current unsorted region, respectively.
function bidirectionalSelectionSort(arr) { let left = 0; let right = arr.length - 1; while (left < right) { // Find minimum and maximum in the current range let minIndex = left; let maxIndex = left; for (let i = left; i <= right; i++) { if (arr[i] < arr[minIndex]) { minIndex = i; } if (arr[i] > arr[maxIndex]) { maxIndex = i; } } // Swap minimum with left element if (minIndex !== left) { [arr[left], arr[minIndex]] = [arr[minIndex], arr[left]]; } // If maxIndex was left, it's now moved to minIndex if (maxIndex === left) { maxIndex = minIndex; } // Swap maximum with right element if (maxIndex !== right) { [arr[right], arr[maxIndex]] = [arr[maxIndex], arr[right]]; } left++; right--; } return arr; }
2. Stable Selection Sort
A modified version of selection sort that preserves the relative order of equal elements.
function stableSelectionSort(arr) { const n = arr.length; for (let i = 0; i < n - 1; i++) { // Find minimum element let minIndex = i; for (let j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // If the minimum is not the current element if (minIndex !== i) { // Save the minimum value const minValue = arr[minIndex]; // Shift all elements between i and minIndex by one position for (let k = minIndex; k > i; k--) { arr[k] = arr[k - 1]; } // Place the minimum at position i arr[i] = minValue; } } return arr; }
This stable version uses more operations (specifically, more assignments) but preserves the relative order of equal elements by shifting elements rather than swapping them.
Conclusion
Selection sort is a simple sorting algorithm with a straightforward implementation. While it's not the most efficient algorithm for large datasets due to its O(n²) time complexity, it has certain advantages like minimizing the number of swaps and using constant auxiliary space.
Key Points to Remember
- Selection sort always performs O(n²) comparisons, making it inefficient for large datasets
- It performs the minimum possible number of swaps (at most n-1)
- It's an in-place algorithm with O(1) space complexity
- The standard implementation is not stable but can be modified to become stable
- It's often outperformed by other algorithms like insertion sort, merge sort, or quick sort
- It serves as a good introduction to sorting algorithms due to its simplicity
Next Steps
To continue your learning about sorting algorithms, explore these related topics:
Related Tutorials
Related Tutorials
Bubble Sort
Another simple sorting algorithm that repeatedly steps through the list.
Learn moreInsertion Sort
Learn another elementary sorting algorithm that builds the sorted array one item at a time.
Learn moreQuick Sort
Explore a more efficient divide-and-conquer sorting algorithm.
Learn more