Summary

Selection sort works in passes. Each pass scans the unsorted part of your array, finds the smallest value then swaps it into the next sorted spot. It repeats until nothing is left to sort. The method is simple and uses no extra memory. But it stays slow on large lists.

Selection sort is the first sorting algorithm most people meet. And it trips up more beginners than it should. Not because the idea is hard. Because nobody shows you what is actually happening inside the array while it runs.

Let me fix that. By the end of this post you will trace selection sort by hand. You will write the code yourself. You will know exactly when to use it and when to walk away.

What is selection sort?

Selection sort is a simple sorting method that builds your sorted list one item at a time. It splits the array into two parts. The sorted part on the left starts empty. Everything else sits in the unsorted part on the right.

Here is the idea in one line. Find the smallest value in the unsorted part then move it to the front of that part. Do this again and again until nothing is left unsorted.

The name tells you what it does. You select the smallest value each round. Then you place it where it belongs.

How does selection sort work step by step?

Reading about selection sort rarely sticks. Watching it move does. So let’s run it on a real array and follow every step.

Take this array of five numbers: [29, 10, 14, 37, 13]. We will walk through it one pass at a time.

Pass one looks at the whole array. It checks every value to find the smallest one. The smallest is 10, sitting at the third spot. Selection sort swaps 10 with the value at the front, which is 29. Now the array reads [10, 29, 14, 37, 13]. The 10 is locked in place. The sorted part is one item long.

Next, pass two ignores the 10 because that spot is done. It scans the rest, from 29 down to 13. The smallest here is 13, so selection sort swaps 13 with 29. The array becomes [10, 13, 14, 37, 29]. Two values are now home.

In pass three the scan starts at 14. It checks 14, then 37, then 29. The smallest is 14, and that value already sits at the front of the unsorted part. So no swap happens this round. The array stays [10, 13, 14, 37, 29]. Selection sort still did the work of checking. It just had nothing to move.

Pass four looks at the last two values, 37 and 29. The smaller one is 29, so selection sort swaps them. The array is now [10, 13, 14, 29, 37]. It is sorted. The final value fell into place on its own, because once everything before it is correct, the last spot has to be right.

The part most tutorials skip

Selection sort makes very few swaps but a lot of comparisons. Sorting these five numbers took ten comparisons but only three real swaps. Pass one compared four times, pass two three, pass three two, pass four one. That swap-versus-comparison tradeoff is the whole personality of this algorithm.

Selection sort code in Python and C++

Once you can trace it, the code is short. Here is selection sort in Python.

def selection_sort(arr):
    n = len(arr)
    for i in range(n - 1):
        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

The outer loop picks the spot to fill. The inner loop hunts for the smallest value to put there. The swap runs once per outer pass, never inside the inner loop.

Here is the same logic in C++, which is the language most Indian placement tests expect.

void selectionSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex])
                minIndex = j;
        }
        int temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
}

The two versions match line for line. Only the syntax changes. Notice that the swap sits outside the inner loop in both. That placement is what keeps the swap count low.

What is the time and space complexity of selection sort?

Selection sort runs in O(n²) time. The reason is the two nested loops. For every item you place, you scan the rest of the list to find the smallest one.

Here is the unusual part. The best case matches the worst case. Even if your array is already sorted, selection sort still scans everything on every pass. It cannot tell that the work is done. Bubble sort can quit early. Selection sort never does.

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

Space is its strong point. Selection sort sorts inside the original array and needs no extra memory. That makes it an in-place algorithm, so the space complexity is O(1).

The comparison count grows fast though. For an array of n items it always makes n times (n minus 1) divided by two comparisons. For 8 items that is 28. For 100 items that is 4,950. The numbers climb whether the list is sorted or not. You can check the formal definition on Wikipedia’s selection sort page.

When should you use selection sort?

Here is the straight answer. In real projects you almost never reach for selection sort. Faster sorts exist. Quick sort and merge sort beat it on anything past a few dozen items. Python, Java and C++ all ship with built-in sort functions that leave it far behind.

So why learn it? Two reasons.

First, it teaches you how sorting thinks. The find the minimum then place it pattern shows up again in harder algorithms. Get it here and the rest make more sense.

Second, it has one real edge. Selection sort makes the fewest swaps of any simple sort. If writing to memory is expensive on your hardware, those few swaps matter. That is a narrow case. But it is a real one.

One more thing to watch

Selection sort is not stable. When two items hold equal values, their original order can flip during a swap. For plain numbers that does not matter. For sorting records by one field while keeping another order intact, it does.

Selection sort vs bubble sort

People mix up selection sort and bubble sort all the time. The difference is simple. Bubble sort swaps neighbours over and over and can stop early once the list is sorted. Selection sort makes one swap per pass and never stops early. For the full side by side, read our bubble sort walkthrough.

FAQ

Is selection sort stable?

No. Equal values can change their original order during a swap. If stability matters, use insertion sort or merge sort instead.

Is selection sort faster than bubble sort?

In practice it often does fewer swaps, which can make it a little faster on data that is costly to move. But both run in O(n²) time, so neither is a good choice for large lists.

What is the time complexity of selection sort?

It is O(n²) in every case, best, average and worst. The nested loops run the same way no matter how the data starts out.

Why does selection sort make so few swaps?

Because it swaps only once per pass, after the inner loop finds the smallest value. Most of its work is comparing, not moving data.

Is selection sort used in real applications?

Rarely. Languages ship with much faster built-in sorts. Selection sort earns its place as a teaching tool and in the narrow case where memory writes are expensive.

Can selection sort work on strings?

Yes. It sorts anything you can compare, so it handles strings, characters or custom objects as long as you define how to compare them.

So what should you remember?

Selection sort is slow but honest. It does the same steady work every time. Find the smallest. Put it in place. Repeat.

Learn it for the pattern, not the speed. Once you can see the array split into a sorted side and an unsorted side, the smarter sorts stop looking scary.

Now try tracing it yourself on a fresh array like [5, 2, 9, 1, 7]. Which value gets locked in first?