Summarise With AI
Back

Understanding the Selection Sort Algorithm

15 Dec 2025
8 min read

What This Blog Covers

  • Explains how the Selection Sort algorithm works, step by step, with clear logic.
  • Breaks down the algorithm, pseudocode, and real code in Python, Java, and C.
  • Shows a manual run-through so you can see exactly how elements move during sorting.
  • Covers time and space complexity with practical explanations, not just formulas.
  • Highlights where Selection Sort is useful and where it fails in today’s programming world.
  • Addresses common mistakes beginners make and how to avoid them.

Introduction

In computer science, sorting algorithms are essential tools that allow data to be arranged in a logical and ordered manner. One​‍​‌‍​‍‌​‍​‌‍​‍‌ of the features that makes Selection Sort stand out among the various sorting algorithms is the fact that it is very simple and has advantages. In fact, it could be considered the least efficient of the sorting algorithms when it comes to large datasets, but its simplicity still makes it an excellent introductory topic for people who are new to algorithm design and ​‍​‌‍​‍‌​‍​‌‍​‍‌analysis.

This blog post will cover the theory behind Selection Sort, its detailed implementation, and its time and space complexities. You will have a comprehensive understanding of Selection Sort's operation and its significance in the broader context of sorting algorithms by the end.

What is Selection Sort?

A simple sorting algorithm known as selection sort always chooses the smallest (or largest) item from the unsorted section and places it in the appropriate location within the sorted section. The list is split into two sections using this method: a sorted segment and an unsorted segment. At first, all of the elements are in the unsorted portion, while the sorted section is empty. In order to essentially increase the sorted section by one and decrease the unsorted portion, the method iteratively selects the least element from the unsorted part and replaces it with the first unsorted element.

Despite being simple to understand and apply, Selection Sort's O(n²) time complexity makes it the least effective method for big datasets. However, because of its simplicity, it is a useful teaching tool for understanding the foundations of sorting algorithms.

Key Features

  • In-Place Sorting – The function of a selection sort is to interchange the elements that are already in the array, hence no extra space is necessary.
  • Comparison-Based – It keeps on comparing elements to determine the smallest (or largest) value.
  • Not Stable – In case there are two equal elements, their relative order may be different from that which it was before the sorting.
  • Time Complexity – Its time complexity is O(n²) in all cases (best, average, and worst) and thus it is not proper for large ​‍​‌‍​‍‌​‍​‌‍​‍‌datasets. 

How Selection Sort Works? 

  1. Find the Minimum – Start by scanning the entire array to find the smallest element.
  2. Swap – Swap this smallest element with the first element of the array.
  3. Repeat – Proceed to the following location and carry out the same procedure for the array's remaining unsorted section.
  4. Continue Until Sorted – The process continues until all elements are placed in the correct order.

Algorithm for Selection Sort

The Selection Sort Algorithm follows these basic steps:

  1. Start with the first element of the array (index 0).
  2. Assume this element is the minimum in the unsorted portion of the array.
  3. Compare this assumed minimum with the remaining elements in the unsorted portion to find the actual minimum value.
  4. If a smaller element is found, update the index of the minimum value.
  5. Once the inner loop is finished, replace the initial element of the unsorted section with the minimum element that was discovered.
  6. Move the boundary of the sorted and unsorted portions one element forward.
  7. Continue doing this until the array is sorted in its entirety.

Manual Example and Run-through

A manual run-through is an effective way to visualize how selection sort operates step by step. By working through a small array, you can see exactly how the algorithm selects the smallest element and places it in its correct position on each pass.

Here is a step-by-step explanation of sorting the array [29, 10, 14, 37, 13] in ascending order using Selection Sort in C:

Initial Array: [29, 10, 14, 37, 13]

We will sort the array by finding the smallest element in the unsorted part and swapping it with the first unsorted element, using the selection sort algorithm.

Step 1: Find the smallest in the whole array

Unsorted portion: [29, 10, 14, 37, 13]

Compare each element:

  • 29>10 → Update smallest to 10.
  • 10<14, 10<37, 10<13 → 10 is still the smallest.

Swap 10 with the first element (29):

 Array after Step 1: [10, 29, 14, 37, 13]

Step 2: Find the smallest in the remaining unsorted part

Unsorted portion: [29, 14, 37, 13]

Compare each element:

  • 29>14 → Update smallest to 1414.
  • 14<37, 14>13 → Update smallest to 1313.

Swap 13 with the first unsorted element (29):

 Array after Step 2: [10, 13, 14, 37, 29]

Step 3: Find the smallest in the remaining unsorted part

Unsorted portion: [14, 37, 29]

Compare each element:

  • 14<37, 14<29 → 14 is already the smallest.

No swap is needed.

 Array after Step 3: [10, 13, 14, 37, 29]

Step 4: Find the smallest in the remaining unsorted part

Unsorted portion: [37, 29]

Compare:

  • 37>29 → Update smallest to 2929.

Swap 29 with 37:

 Array after Step 4: [10, 13, 14, 29, 37]
Final Sorted Array: [10, 13, 14, 29, 37]

Each step confirms that the smallest element is placed in its correct position by the algorithm for selection sort, and the process continues until the array is sorted.

Pseudocode and Implementation of Selection Sort

Selection sort can be implemented in a variety of programming languages, but the underlying logic remains the same. Below, you'll find clear pseudocode as well as sample implementations in Python, Java, and C.

for i = 0 to n-1:
    min_index = i
    for j = i+1 to n:
        if arr[j] < arr[min_index]:
            min_index = j
    Swap arr[i] and arr[min_index]

Implementation of Selection Sort in Python

Selection Sort can be easily implemented in Python using basic loops and comparisons. It repeatedly finds the smallest element and swaps it to its correct position.

Python Code

Below is a simple Python program that explains the function of Selection Sort. The code sorts the list of numbers in an ascending order by the use of nested ​‍​‌‍​‍‌​‍​‌‍​‍‌loops.

# Python program to implement Selection Sort
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_index = i
        for j in range(i+1, n):
            if arr[j] < arr[min_index]:  # Finding the smallest element
                min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]  # Swapping
    return arr

# Example usage
arr = [64, 25, 12, 22, 11]
print("Sorted array:", selection_sort(arr))

Output:

Sorted array: [11, 12, 22, 25, 64]

Implementation of Selection Sort in Java

Selection Sort in Java is implemented using nested loops to find the minimum element and swap it with the current position. This method effectively sorts the array in ascending order step-by-step.

Java Code

The below Java code for Selection Sort uses a for-loop to iterate over the array and an inner loop to find the smallest element. After locating it, the elements are swapped to sort the array.

// Java program to implement Selection Sort
import java.util.Arrays;

public class SelectionSort {
    public static void selectionSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) { // Finding the smallest element
                    minIndex = j;
                }
            }
            // Swapping elements
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        selectionSort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }
}

Output:

Sorted array: [11, 12, 22, 25, 64]

Implementation of Selection Sort in C

Selection Sort in C is done with the help of nested loops where the minimum element is found and then swapped with the current index. This loop continues till the whole array is sorted in ascending order.

C Code

The program in C for Selection Sort employs for loops to go through the array and to do the swapping when a smaller element is found. The straightforward logic here is to sort the array gradually, which is done quite ​‍​‌‍​‍‌​‍​‌‍​‍‌efficiently.

// C program to implement Selection Sort
#include <stdio.h>

// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
    int i, j, min_index, temp;
    
    for (i = 0; i < n-1; i++) {
        min_index = i;
        
        // Find the smallest element in the unsorted portion
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_index]) {
                min_index = j;
            }
        }
        
        // Swap the smallest element with the first unsorted element
        temp = arr[min_index];
        arr[min_index] = arr[i];
        arr[i] = temp;
    }
}

// Function to print the array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// Main function
int main() {
    int arr[] = {64, 25, 12, 22, 11};
    int n = sizeof(arr) / sizeof(arr[0]);

    selectionSort(arr, n);
    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}

Output:

Sorted array: 11 12 22 25 64

Bottom Line: These examples show how the selection sort pseudocode translates into actual code in different programming languages. The core logic, finding the minimum element in the unsorted part of the array and swapping it into place, remains consistent across implementations.

Complexity Analysis of Selection Sort Algorithm

Understanding the complexity analysis of Selection Sort helps you decide when to use it and when to avoid it. Although the algorithm is simple, its performance characteristics are important from both academic and practical perspectives.

Time Complexity of Selection Sort

Selection Sort repeatedly determines the minimal element from the array's unsorted section using nested loops.

  • The outer loop runs n−1 times.
  • The inner loop scans the remaining unsorted elements each time.

Because of these nested loops, the total number of comparisons grows quadratically with the size of the input.

Best Case Time Complexity

  • O(n²)
  • Selection Sort compares each element, even if the array has previously been sorted.
  • There is no early stopping condition.

Average Case Time Complexity

  • O(n²)
  • For randomly ordered data, the number of comparisons remains the same.

Worst Case Time Complexity

  • O(n²)
  • Reverse-sorted arrays perform the same number of comparisons as any other case.

Key Insight

Selection Sort is non-adaptive, the input order does not affect its running time.

Space Complexity of Selection Sort

  • O(1) space complexity
  • The algorithm does not require additional memory, except for a few variables for indexing and swapping. The sorting is done in-place, directly within the original array.
  • Sorting is done in-place, directly within the original array.

Why This Matters

Selection Sort is useful in environments where memory is limited, even though it is slow for large inputs.

Number of Comparisons and Swaps

  • Comparisons: Always around n(n−1)/2 → O(n²)
  • Swaps: At most n−1

This has fewer swaps than Bubble Sort, which may swap elements many times.

Comparison of Selection Sort Algorithm with Bubble Sort

Aspect Selection Sort Bubble Sort
Time Complexity O(n²) O(n²)
Best Case Optimization No Yes (using a swap flag)
Number of Swaps Minimal (n − 1) High
Stability Not stable Stable
Suitable for Large Data Sets No No

Bottom Line:

While Selection Sort does fewer swaps, it doesn't have the early-exit feature that Bubble Sort can use. 

Behavior on Large Data Sets

Because of its quadratic time complexity, Selection Sort is not efficient with large data sets. It becomes increasingly slow with more elements and thus is not suitable for large-scale applications in the real world.

Run Time Graph Explanation

If you plot input size (n) on the X-axis and run time on the Y-axis:

  • The graph forms a parabolic (curved) shape
  • Doubling the input size increases run time by roughly four times

This visual clearly explains why Selection Sort does not scale well.

Summary

  • Time Complexity: O(n²) for best, average, and worst cases
  • Space Complexity: O(1), no additional memory is needed
  • Strength: Few swaps, straightforward logic
  • Weakness: Not capable of handling a large data set efficiently 

Selection Sort Algorithm is not the right choice for performance-critical applications, but it is good for learning, small inputs, or memory-constrained systems.

Advantages of Selection Sort Algorithm

Selection Sort offers several benefits that make it a very good option in certain situations. Here are some of the key advantages of using an algorithm for selection sort:

1. Simplicity:

Selection Sort is among the simplest sorting algorithms, making it easy to understand and implement. Its direct approach of repeatedly finding the minimum element and swapping it with the current position confirms clarity in both logic and coding. This simplicity also makes it an excellent choice for beginners learning sorting algorithms.

2. In-Place Sorting:

The algorithm doesn't need any additional memory or auxiliary data structures for sorting. Since it sorts the array in place by swapping elements, it has a space complexity of O(1), which is beneficial in memory-constrained systems.

3. Effectiveness for Small Datasets:

The Selection Sort Algorithm is a good choice for a small dataset in which the inefficiencies of the algorithm in terms of the number of comparisons and swaps do not affect the performance significantly. If a case is that simplicity and a low memory usage are more important than the speed, then this algorithm is a proper one.

4. Predictable Performance:

Selection Sort always makes the same number of comparisons regardless of the initial order of elements. Therefore, its operation is predictable and can be easily accounted for, which is particularly great for real-time systems that require a consistent execution time.

5. Minimal Number of Swaps:

Selection Sort, as compared to other simple sorting algorithms like Bubble Sort, executes less swaps, thus, it lessens the impact of the system where writing to memory or storage is costly, e.g., flash memory or ​‍​‌‍​‍‌​‍​‌‍​‍‌EEPROM.

Disadvantages of Selection Sort Algorithm

While the algorithm for selection sort has its advantages, it also comes with certain drawbacks that make it less suitable for other situations. Here are some of the key disadvantages of using this sorting algorithm:

1. Inefficiency for Large Datasets:

In the best, medium, and worst scenarios, Selection Sort's time complexity is O(n^2). This quadratic time complexity means that its performance reduces rapidly as the size of the dataset increases, which makes it unsuitable for sorting large datasets.

2. Lack of Stability:

The sorting algorithm Selection Sort is unstable. Stability refers to maintaining the order of elements with equal keys. Since the algorithm swaps elements, it can disturb the original order of identical elements, which can be undesirable in certain applications.

3. Non-Adaptive Nature:

The algorithm performs the same number of comparisons and swaps as the initial arrangement of elements. This non-adaptive behavior makes it inefficient for datasets that are already sorted or entirely sorted.

4. Poor Cache Performance:

Selection Sort doesn’t use computer memory efficiently because it jumps around the list a lot. This makes it slower on big lists compared to other sorting methods that go through data more smoothly.

5. Not Good for Real-Time Tasks:

This kind of sorting is inefficient, and it will always require the same amount of time, even if the list is sorted almost. For this reason, it is not a good choice when something has to be quickly sorted, as in real-time systems.

Applications of Selection Sort Algorithm

Selection Sort Algorithm is useful in specific contexts where its simplicity and characteristics are beneficial. Here are some common applications where this algorithm can be effectively used:

1. Educational Purposes:

Selection Sort is one of the first concepts taught in computer science courses when introducing sorting algorithms. Its simplicity helps students grasp basic concepts such as comparison, swapping, and algorithmic analysis.

2. Small Datasets:

Where datasets are small, the use of more complex algorithms is not justified due to their overhead. The simplicity of Selection Sort and the ease of its implementation make it the perfect method for sorting small collections when performance is not a critical ​‍​‌‍​‍‌​‍​‌‍​‍‌factor.

3. Memory-Constrained Environments:

Since Selection Sort has a space complexity of O(1), it is particularly useful in environments where memory resources are limited. For embedded systems or low-memory devices, its low-memory requirements outweigh its inefficiency for larger datasets.

4. When Write Operations Are Costly:

Selection Sort performs the minimum number of swaps compared to other simple sorting algorithms like Bubble Sort. This makes it suitable for situations where write operations (such as to flash memory) are expensive or limited.

5. Partial Sorting Needs:

In​‍​‌‍​‍‌​‍​‌‍​‍‌ some instances, it might be sufficient to only sort or identify the first few smallest elements instead of the whole dataset. Selection Sort can be a good choice in such cases, as it selects the minimum elements with each pass and thus allows early access to partially sorted results without the need for a complete sorting of the list.

Common Issues and Solutions in Selection Sort

Selection Sort, while simple in concept, beginners sometimes struggle with practical issues in the process of its implementation. Identifying these problems along with their solutions can guide you towards writing code that is both cleaner and more efficient.

1. Shifting Instead of Swapping Elements

Issue:

Newbies​‍​‌‍​‍‌​‍​‌‍​‍‌ sometimes take out the minimum element and move the rest elements as if the minimum element is at the front. Such doing leads to the unnecessary shifting operations i.e. the write operations are increased, which results in the algorithm becoming slower.

Solution:

Always swap the minimum element with the first unsorted element. Swapping is a constant-time operation and is the standard approach in Selection Sort.

Why it matters:

Less swapping leads to better performance, which is especially true for scenarios in which memory writes are costly.

2. Change in Relative Order of Equal Elements (Stability Issue)

Issue:

Selection Sort is an unstable sorting algorithm. When two identical elements are encountered, their swapping can result in the change of the relative order of equal elements, which may lead to problems if sorting records with multiple fields.

Solution:

When stability is a requirement, switching to a stable sorting algorithm like Insertion Sort or Merge Sort is the best option. Only when the preservation of the order is not necessary should Selection Sort be considered. 

Key takeaway:

The algorithm Selection Sort sacrifices stability for the sake of simplicity.

3. Unnecessary Swaps

Issue:

Beginners are likely to perform swaps on elements even in cases when the minimum element is already in its proper position thus, artificially increasing the number of swaps.

Solution:

It never damages to check if the minimum index differs from the current index before you make the move of swapping. Perform the swap only when it's necessary.

Benefit:

This leads to a decrease in unnecessary memory writes and, thus, a slight improvement in efficiency.

4. Misunderstanding the Worst-Case Scenario

Issue:

It is commonly thought that Selection Sort works faster on sorted arrays. In fact, it still carries out the same number of comparisons regardless of the situation.

Worst Case Scenario:

Selection Sort will still take O(n²) time for sorted or reverse-sorted arrays.

Solution:

Realize that Selection Sort is a non-adaptive sorting algorithm. For large or almost sorted datasets, it would be better to use algorithms such as Quick Sort or Merge ​‍​‌‍​‍‌​‍​‌‍​‍‌Sort.

5. Confusing Comparisons with Swaps

Issue:

Selection Sort makes a lot of comparisons and a few swaps. Beginners, however, tend to mix up these two operations when they look at the ​‍​‌‍​‍‌​‍​‌‍​‍‌performance.

Solution:

Remember:

  • Comparisons → Always O(n²)
  • Swaps → At most n−1

Why this matters:

Selection Sort is sometimes chosen when minimizing swaps is more important than reducing comparisons.

Bottom Line

The Selection Sort Algorithm is a simple method that is obvious to have certain limitations. Try replacing shifting with swapping, be aware of its stability limitations, and accept that its worst-case situation cannot be avoided. Essentially, it is a good tool for grasping ideas, working with small datasets, or a memory-constrained system where memory writes are expensive, rather than for high-performance sorting ​‍​‌‍​‍‌​‍​‌‍​‍‌tasks.

Conclusion

Selection​‍​‌‍​‍‌​‍​‌‍​‍‌ Sort is a easy sorting algorithm that only takes minimal effort to comprehend and execute. The method involves finding the minimum element over and over and then putting it into its correct place. But because of its O(n^2) time complexity, the Selection Sort algorithm is still very slow when dealing with large amounts of data. Nevertheless, it is still important in a memory-constrained environment because of its in-place sorting method and little memory consumption. Although it is not stable and not adaptive, it is still a good tool for the introduction of the sorting concept and for small datasets. The use of efficient algorithms like QuickSort or MergeSort is better for large ​‍​‌‍​‍‌​‍​‌‍​‍‌datasets. 

Points to Remember

  1. Selection Sort always scans the full unsorted portion, even if the array is already sorted. Input order doesn’t change its speed.
  2. The time complexity is always O(n²). The best, average, and worst cases are identical; thus, it is very limited in its practical application.
  3. The space complexity is O(1) as it does an in-place sort of the array without the need for additional memory.
  4. It performs very few swaps (at most n−1), which makes it useful in systems where write operations are expensive.
  5. Selection Sort is mostly a learning algorithm, perfect for grasping sorting concepts, but not appropriate for large datasets or performance-critical ​‍​‌‍​‍‌​‍​‌‍​‍‌systems. 

Frequently Asked Questions

1. Why is Selection Sort called "Selection" Sort?

The method is called that way as it keeps on selecting the minimum (or maximum) element from the unsorted part of the array and putting it in the sorted part.

2. Is Selection Sort a stable sorting algorithm?

No, Selection Sort is not stable as the swapping of elements may change the relative order of identical elements in the array.

3. When should I use Selection Sort?

You should use Selection Sort for a small dataset or when memory efficiency is more important than speed because it only requires O(1)O(1) additional space.

4. What is the main disadvantage of Selection Sort?

The main disadvantage of the algorithm is its O(n2)O(n^2) time complexity, which makes it very slow for large datasets compared to efficient sorting algorithms such as QuickSort or MergeSort.

5. How many swaps does Selection Sort perform?

It does at most n−1n-1 swaps, which is less than the number of swaps performed by Bubble Sort, thus making it a more efficient algorithm in terms of swapping operations.

6. Is Selection Sort better than Bubble Sort?

Yes, Selection Sort generally performs fewer swaps than Bubble Sort, making it slightly more efficient, but both have the same O(n2)O(n^2) time complexity.

7. Can Selection Sort be used for linked lists?

While it can be implemented for linked lists, other algorithms like MergeSort are preferred because they perform better with the linked list data structure.

Summarise With Ai
ChatGPT
Perplexity
Claude
Gemini
Gork
ChatGPT
Perplexity
Claude
Gemini
Gork
Chat with us
Chat with us
Talk to career expert