Selection Sort

Data Structure and Algorithm 280 views Nov 05, 2025 1 min read

Selection Sort Algorithm

Selection Sort repeatedly selects the smallest element from the unsorted part and places it at the beginning.

Algorithm Steps

  1. Find the minimum element in the unsorted array.
  2. Swap it with the first unsorted element.
  3. Repeat for all elements.

Example (C++)

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;
        swap(arr[minIndex], arr[i]);
    }
}

Time Complexity

  • Best, Average, Worst: O(n²)

Advantages

  • Simple and works well on small datasets.

Disadvantages

  • Does not adapt to data.

Conclusion

Selection sort is conceptually simple but inefficient for large datasets.

Some advanced sections are available for Registered Members
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials