DSA — Selection Sort
What is Selection Sort?
Finds the minimum element and places it at the beginning, repeating for each position.
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
How it Works
[64, 25, 12, 22, 11]
↓
[11, 25, 12, 22, 64]
↓
[11, 12, 25, 22, 64]
↓
[11, 12, 22, 25, 64]
Time Complexity
| Case | Complexity |
|---|---|
| Best | O(n²) |
| Average | O(n²) |
| Worst | O(n²) |
Characteristics
- Not stable
- In-place
- Simple to implement
- Always O(n²) comparisons
Mini Practice
- Implement selection sort
- Sort in descending order
- Find number of swaps
- Compare with bubble sort
Up Next
Continue with Insertion Sort — insertion sorting algorithm.
Related Topics
Frequently Asked Questions about Selection Sort
What is Selection Sort in DSA?
Selection Sort is a fundamental concept in DSA. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Selection Sort?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Selection Sort.
Why is Selection Sort important in DSA?
Selection Sort is essential for DSA development. Understanding this concept will help you write better code and solve real-world problems more effectively.