</>
Skip to content
DSA lessons (31/55)

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

CaseComplexity
BestO(n²)
AverageO(n²)
WorstO(n²)

Characteristics

  • Not stable
  • In-place
  • Simple to implement
  • Always O(n²) comparisons

Mini Practice

  1. Implement selection sort
  2. Sort in descending order
  3. Find number of swaps
  4. 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.