#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Sorting Algorithms

Selection Sort Algorithm

Selection Sort divides the array into a sorted and an unsorted region. In each iteration, it selects the smallest (or largest) element from the unsorted region and swaps it into its correct position at the boundary of the sorted region.

Algorithm Steps

  • Set the first element as the current minimum.
  • Scan the rest of the array to find the actual minimum element.
  • Swap the minimum found with the first unsorted element.
  • Move the boundary of the sorted region forward by one and repeat.
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

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

Complexity

CaseTimeSpace
Best/Average/WorstO(n²)O(1)

Key Characteristics

  • Not stable by default (equal elements may change relative order).
  • Performs a minimal number of swaps — exactly n-1 in the worst case — useful when write operations are expensive.
  • Simple to implement and understand, making it common in teaching contexts.

Ready to master Data Structures & Algorithms?

Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.

Explore Course