Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending or Descending). In selection sort, the first element in the list is selected and it is compared repeatedly with all the remaining elements in the list. If any element is smaller than the selected element (for Ascending order), then both are swapped so that first position is filled with the smallest element in the sorted order. Next, we select the element at a second position in the list and it is compared with all the remaining elements in the list. If any element is smaller than the selected element, then both are swapped. This procedure is repeated until the entire list is sorted.
Step by Step Process
The selection sort algorithm is performed using the following steps…
- Step 1: Select the first element of the list (i.e., Element at first position in the list).
- Step 2: Compare the selected element with all the other elements in the list.
- Step 3: In every comparision, if any element is found smaller than the selected element (for Ascending order), then both are swapped.
- Step 4: Repeat the same procedure with element in the next position in the list till the entire list is sorted.
Following is the sample code for selection sort…
Selection Sort Logic
for(i=0; i<size; i++){
for(j=i+1; j<size; j++){
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
SELECTION-SORT (A)
START
n ← Length[A]
for j ← 1 to n-1
smallest ← j
for i← j+1 to n
if A[i] < A[smallest]
then smallest ← i
exchange(A[j], A[smallest])
STOP
Example

Time Complexities:
- Best Case Complexity: The selection sort algorithm has a best-case time complexity of Ω(n2) for the already sorted array.
- Average Case Complexity: The average-case time complexity for the selection sort algorithm is Θ(n2), in which the existing elements are in jumbled ordered, i.e., neither in the ascending order nor in the descending order.
- Worst Case Complexity: The worst-case time complexity is also O(n2), which occurs when we sort the descending order of an array into the ascending order.
