Selection Sort Algorithm
Selection Sort divides the array into a sorted and an unsorted region. In each iteration, it selects the smallest (or largest) element from the unsorted region and swaps it into its correct position at the boundary of the sorted region.
Algorithm Steps
- Set the first element as the current minimum.
- Scan the rest of the array to find the actual minimum element.
- Swap the minimum found with the first unsorted element.
- Move the boundary of the sorted region forward by one and repeat.
def selection_sort(arr):
n = len(arr)
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr
print(selection_sort([64, 25, 12, 22, 11]))
Complexity
| Case | Time | Space |
|---|---|---|
| Best/Average/Worst | O(n²) | O(1) |
Key Characteristics
- Not stable by default (equal elements may change relative order).
- Performs a minimal number of swaps — exactly n-1 in the worst case — useful when write operations are expensive.
- Simple to implement and understand, making it common in teaching contexts.
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)