DSA Tracker

Easy

Selection Sort

An easy Sorting problem included in Apna College, Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Sorting
Sheets
2
Core for
13 roles
Platform
GeeksforGeeks

The problem

Sort an array by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning.

Example 1

Input
nums = [64, 25, 12, 22, 11]
Output
[11, 12, 22, 25, 64]

Example 2

Input
nums = [5, 3, 8, 4, 2]
Output
[2, 3, 4, 5, 8]

Constraints

  • 1 <= nums.length <= 100
  • -10^4 <= nums[i] <= 10^4

How to think about it

Updated 2026-09-09

Instead of moving values around opportunistically, commit to solving slot i definitively before looking at slot i + 1. Scanning the remaining suffix finds the global minimum of that window, so a single exchange places the correct item into its permanent home and never touches that slot again.

Approaches, worst first

  1. Copy to fresh array

    time O(n^2) · space O(n)

    Find the smallest element in the collection, append it to a new result list, and mark or delete it from the original. Simple to trace, but allocating extra storage or shifting elements on deletion adds unnecessary overhead.

  2. In-place suffix scanWrite this one

    time O(n^2) · space O(1)

    Maintain the boundary between sorted prefix and unsorted suffix. Walk the suffix to locate the index of the minimum element, then execute a single swap with the boundary slot. It minimizes writes to exactly n - 1 swaps total.

Where people lose marks · 3
  • Swapping values immediately during the inner scan turns selection sort into an inefficient bubble sort; only the index of the minimum should update until the pass completes.
  • Assuming selection sort is stable by default: swapping the minimum across long distances leaps over duplicate values and inverts their relative order.
  • Running the outer loop through n - 1 rather than stopping at n - 2 is harmless, but executing a swap when the minimum index is already the current index is a redundant write.

The theory behind it

Sorting — the ground this problem stands on. All Sorting problems

What Sorting is

Sorting is the act of arranging a scrambled hand of playing cards into ascending rank from left to right. It reorganizes scattered data according to a consistent comparison rule, like numbering index cards or alphabetizing names. While unsorted data requires searching every single entry to verify whether an item exists, ordered data establishes predictable relationships that make duplicates, clusters, and extreme values immediately visible.

When to reach for it

Reach for sorting when a problem asks to group identical items, detect overlaps among intervals, find rank percentiles, or pair values matching a target sum. If an unordered problem appears intractable in polynomial time, sorting the input frequently unlocks linear scans or two-pointer sweeps. When an O(n log n) preprocessing step simplifies downstream matching logic, sorting is usually the right opening move.

How the pattern works

Think of sorting as a trade: invest logarithmic overhead upfront to make subsequent queries direct and orderly. Compare adjacent elements to uncover duplicate entries, or march inward from outer boundaries once elements stand in monotonic sequence. When designing custom comparators, confirm strict weak ordering by verifying reflexivity, antisymmetry, and transitivity; inconsistent comparison logic breaks internal pivot partitions or produces corrupted outputs.

What each operation costs

OperationTime
sort using comparison based algorithmsO(n log n)
sort bounded integers using count bucketsO(n + k)
sort using quadratic bubble or selectionO(n^2)
What usually goes wrong with Sorting
  • Writing comparator functions that return inconsistent ordering results, violating transitive rules and leading to infinite loops or crashes during library sorting.
  • Sorting in-place when original array indices must be returned in the final answer, destroying initial positions without keeping index-value pairings beforehand.
  • Assuming default language sorting sorts numbers numerically when some environments convert arguments to strings first, sorting ten ahead of two.

Which roles need this problem

Sorting is a core topic for these 13 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 13 more roles, including ML Engineer, Android Developer, iOS Developer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

MedlifeMicrosoft

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Sorting problems

Problem set and role mapping as of .