Python Tutorial

Selection Sort

Repeatedly select the smallest remaining element and place it at the front of the unsorted part.

How Selection Sort Works

Divide the list into a sorted part (initially empty) at the front and an unsorted part behind it. On each pass, scan the unsorted part for the minimum and swap it into the first unsorted position. After k passes the first k elements are final.

It always makes O(n²) comparisons but performs at most n−1 swaps — useful when writing to memory is expensive.

Implementation

def selection_sort(arr):
    n = len(arr)
    for i in range(n - 1):
        min_idx = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j            # track the smallest so far
        if min_idx != i:
            arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return arr

print(selection_sort([64, 25, 12, 22, 11]))
# [11, 12, 22, 25, 64]

Step-by-Step

Sorting [64, 25, 12, 22, 11]:

PassMin foundArray after swap
111[11, 25, 12, 22, 64]
212[11, 12, 25, 22, 64]
322[11, 12, 22, 25, 64]
425[11, 12, 22, 25, 64]

Complexity

MetricValue
Comparisons (all cases)O(n²)
SwapsO(n) — at most n−1
SpaceO(1) in place
Stable?No (the standard swap version)

Selection vs Bubble vs Insertion

  • Selection: fewest swaps (good when writes are costly), but never better than O(n²) comparisons.
  • Bubble: many swaps; O(n) best case if optimized.
  • Insertion: excellent on nearly-sorted data, O(n) best case, and stable.

Selection sort minimizes the number of writes to the array — a real advantage on hardware like flash memory where writes wear out cells.

Best Practices

  • Use it to teach the "find-the-minimum" pattern, not for production sorting.
  • Prefer it over bubble sort when write count matters more than comparison count.
  • For real code, use sorted() / list.sort().

Try It Yourself

Exercise 1: How many swaps (at most) does selection sort perform on n elements?

Show solution

At most n − 1 — one swap per pass. This is its key advantage when writes are expensive.

Exercise 2: Is standard selection sort stable?

Show solution

No — swapping distant elements can change the relative order of equal keys.

📘 Real-World Deep Dive

Knowing <strong>DSA Selection Sort (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Selection Sort that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

def selection(xs):
    for i in range(len(xs)):
        m = i
        for j in range(i+1, len(xs)):
            if xs[j] < xs[m]: m = j
        xs[i], xs[m] = xs[m], xs[i]
    return xs

print(selection([29, 10, 14, 37, 13]))

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Selection Sort as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Selection Sort

Common questions about this page.

What is Selection Sort?

Selection Sort is a DSA lesson that explains selection sort in Python. Repeatedly select the smallest remaining element and place it at the front of the unsorted part. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run selection sort examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn selection sort in this DSA Python lesson (Selection Sort).

How do I use selection sort in Python?

To use selection sort in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of selection sort?

This Selection Sort tutorial shows selection sort syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Selection Sort example for beginners

Yes. This page includes a beginner selection sort example you can copy and run. It is designed for searches such as "selection sort for beginners", "selection sort example", and "how to use selection sort".

What are common mistakes with selection sort?

Common selection sort mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn selection sort?

Selection Sort is used in real Python work. Learning selection sort helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Selection Sort free to learn online?

Yes. You can learn selection sort free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.