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

  1. Initialize the sorted portion as empty.
  2. From the unsorted portion, find the minimum element.
  3. Swap the minimum with the first unsorted element.
  4. Expand the sorted portion and shrink the unsorted portion.
  5. Repeat until the entire array is sorted.

Selection Sort Algorithm: Find Minimum and Swap Approach Explained with Examples

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].

  1. Iteration 1: Minimum is 10, swap with 29 β†’ [10, 29, 14, 37, 13]
  2. Iteration 2: Minimum is 13, swap with 29 β†’ [10, 13, 14, 37, 29]
  3. Iteration 3: Minimum is 14, already in place β†’ [10, 13, 14, 37, 29]
  4. Iteration 4: Minimum is 29, swap with 37 β†’ [10, 13, 14, 29, 37]
  5. Iteration 5: Sorted array achieved.

Selection Sort Algorithm: Find Minimum and Swap Approach Explained with Examples

Visual Walkthrough of Comparing and Swapping

Selection Sort Algorithm: Find Minimum and Swap Approach Explained with Examples

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

Selection Sort Algorithm: Find Minimum and Swap Approach Explained with Examples

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.