For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Selection Sort in Data Structures - Infovistar

Selection Sort

Selection sort is a simple sorting algorithm. In selection sort, the lowest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position in the array.

It is an in-place comparison sorting algorithm. In this algorithm, the array is divided into two parts. The first is the sorted part, and the second is the unsorted part.

Initially, the sorted part of the array is empty, and the unsorted part is the given array. The sorted part is on the left, while the unsorted part is on the right.

In selection sort, the lowest element is selected from the unsorted array and placed at the top of the list. Next, the second lowest element is selected and placed in the second position.

The process continues until the array has been sorted completely.

Time Complexity

Case Time Complexity
Best Case O(n2)
Average Case O(n2)
Worst Case O(n2)
  • Best Case O(n2):It occurs when there is no sorting needed, i.e. the array is already sorted.
  • Average Case O(n): The array elements are in a jumbled order that is not properly ascending and descending.
  • Worst Case O(n): This occurs when the array elements need to be sorted in reverse order. In other words, suppose you have to sort an array in ascending order, but its elements are arranged in descending order.

Space Complexity

Space Complexity O(1)
Stable Yes
  • A selection sort required extra variables for swapping. Hence, the space complexity of selection sorting is O(1).

Selection Sort