Python Program for Selection Sort

In this article, we will learn about the selection sort algorithm and its implementation in Python. Selection sort is a simple comparison-based sorting algorithm that works by finding the minimum element from the unsorted portion and placing it at the beginning.

How Selection Sort Works

The selection sort algorithm maintains two subarrays during execution:

  • The subarray which is already sorted (initially empty)
  • The subarray which is unsorted (initially the entire array)

During each iteration, selection sort finds the minimum element from the unsorted subarray and swaps it with the first element of the unsorted portion.

Selection Sort Process Initial: 64 25 12 22 11 Step 1: 11 25 12 22 64 Step 2: 11 12 25 22 64 Final: 11 12 22 25 64 Sorted Unsorted

Implementation

Here's the Python implementation of selection sort ?

def selection_sort(arr):
    n = len(arr)
    
    # Traverse through all array elements
    for i in range(n):
        # Find the minimum element in remaining unsorted array
        min_idx = i
        for j in range(i+1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        
        # Swap the found minimum element with the first element
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    
    return arr

# Example usage
numbers = [64, 25, 12, 22, 11, 90]
print("Original array:", numbers)

sorted_numbers = selection_sort(numbers)
print("Sorted array:", sorted_numbers)
Original array: [64, 25, 12, 22, 11, 90]
Sorted array: [11, 12, 22, 25, 64, 90]

Step-by-Step Example

Let's trace through the algorithm with a string array ?

def selection_sort_with_steps(arr):
    n = len(arr)
    print(f"Initial array: {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
        
        # Swap elements
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
        print(f"After step {i+1}: {arr}")
    
    return arr

# Sort characters
letters = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
selection_sort_with_steps(letters)
Initial array: ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
After step 1: ['a', 'u', 't', 'o', 'r', 'i', 't', 'l']
After step 2: ['a', 'i', 't', 'o', 'r', 'u', 't', 'l']
After step 3: ['a', 'i', 'l', 'o', 'r', 'u', 't', 't']
After step 4: ['a', 'i', 'l', 'o', 'r', 'u', 't', 't']
After step 5: ['a', 'i', 'l', 'o', 'r', 'u', 't', 't']
After step 6: ['a', 'i', 'l', 'o', 'r', 't', 'u', 't']
After step 7: ['a', 'i', 'l', 'o', 'r', 't', 't', 'u']
After step 8: ['a', 'i', 'l', 'o', 'r', 't', 't', 'u']

Algorithm Analysis

Complexity Type Best Case Average Case Worst Case
Time Complexity O(n²) O(n²) O(n²)
Space Complexity O(1)

Key Points

  • Selection sort is not stable − it may change the relative order of equal elements
  • It performs the same number of comparisons regardless of input order
  • It makes exactly n-1 swaps, which is optimal for algorithms that swap elements
  • It's an in-place sorting algorithm requiring only O(1) extra memory

Conclusion

Selection sort is a simple sorting algorithm with O(n²) time complexity. While not efficient for large datasets, it's useful for small arrays and educational purposes due to its simplicity and minimal memory usage.

Updated on: 2026-03-25T06:25:28+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements