The Selection Sort algorithm is one of the simplest sorting algorithms that follows the concept of repeatedly finding the minimum element and placing it at its correct position. While it may not be the most efficient for large datasets, its concept of find minimum and swap makes it an excellent choice for learning the basics of sorting.
In this article, we will break down Selection Sort: Find Minimum and Swap Approach in detail using step-by-step dry runs, Python examples, complexity analysis, and visual explanations.
What is Selection Sort Algorithm?
Selection Sort is a comparison-based sorting algorithm that works by dividing the list into two parts: a sorted part at the beginning and an unsorted part at the end. During each iteration, the algorithm finds the minimum element from the unsorted part and swaps it with the first element of the unsorted part.
Key Idea of Selection Sort
- Start with the first position in the array.
- Find the smallest element in the unsorted portion.
- Swap it with the element at the current position.
- Repeat until the array is fully sorted.
Algorithm Steps: Find Minimum and Swap Approach
- Initialize the sorted portion as empty.
- From the unsorted portion, find the minimum element.
- Swap the minimum with the first unsorted element.
- Expand the sorted portion and shrink the unsorted portion.
- Repeat until the entire array is sorted.
Python Implementation of Selection Sort
def selection_sort(arr):
n = len(arr)
for i in range(n):
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
# Example
data = [64, 25, 12, 22, 11]
print("Unsorted Array:", data)
print("Sorted Array:", selection_sort(data))
Step-by-Step Example of Selection Sort
Let’s take an example array: [29, 10, 14, 37, 13].
- Iteration 1: Minimum is
10, swap with29β[10, 29, 14, 37, 13] - Iteration 2: Minimum is
13, swap with29β[10, 13, 14, 37, 29] - Iteration 3: Minimum is
14, already in place β[10, 13, 14, 37, 29] - Iteration 4: Minimum is
29, swap with37β[10, 13, 14, 29, 37] - Iteration 5: Sorted array achieved.
Visual Walkthrough of Comparing and Swapping
Interactive Selection Sort in Python
Hereβs a Python interactive step simulator you could run:
def selection_sort_steps(arr):
n = len(arr)
for i in range(n):
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]
print("Step", i+1, ":", arr)
data = [29, 10, 14, 37, 13]
selection_sort_steps(data)
Output:
Step 1 : [10, 29, 14, 37, 13] Step 2 : [10, 13, 14, 37, 29] Step 3 : [10, 13, 14, 37, 29] Step 4 : [10, 13, 14, 29, 37] Step 5 : [10, 13, 14, 29, 37]
Time and Space Complexity of Selection Sort
- Time Complexity:
- Best Case: O(n2)
- Average Case: O(n2)
- Worst Case: O(n2)
- Space Complexity: O(1) since no extra memory is used apart from temporary variables.
Advantages of Selection Sort
- Conceptually simple and easy to implement.
- Useful for small datasets.
- In-place sorting (no additional memory needed).
Disadvantages of Selection Sort
- Not efficient for large datasets due to O(nΒ²) complexity.
- Does not adapt well to partially sorted data.
- Performs unnecessary swaps compared to other algorithms.
Selection Sort vs Other Sorting Algorithms
Conclusion
The Selection Sort algorithm follows the intuitive method of finding the minimum and placing it at the right position step by step. While not optimal for large datasets, it is an excellent way to understand the essence of sorting algorithms. Its Find Minimum and Swap approach makes it beginner-friendly, and through visual and code examples we can clearly see how arrays get sorted. For real-world applications, more efficient algorithms like Merge Sort, Heap Sort, and Quick Sort are preferred, but Selection Sort remains a classic in learning algorithm basics.
- What is Selection Sort Algorithm?
- Algorithm Steps: Find Minimum and Swap Approach
- Python Implementation of Selection Sort
- Step-by-Step Example of Selection Sort
- Visual Walkthrough of Comparing and Swapping
- Interactive Selection Sort in Python
- Time and Space Complexity of Selection Sort
- Advantages of Selection Sort
- Disadvantages of Selection Sort
- Selection Sort vs Other Sorting Algorithms
- Conclusion







