Search

# Selection Sort

Selection sort is a simple comparison-based sorting algorithm. It is in-place and needs no extra memory.

The idea behind this algorithm is pretty simple. We divide the array into two parts: sorted and unsorted.

The left part is sorted subarray and the right part is unsorted subarray. Initially, sorted subarray is empty and unsorted array is the complete given array.

We perform the steps given below until the unsorted subarray becomes empty:

1. Pick the minimum element from the unsorted subarray.

2. Swap it with the leftmost element of the unsorted subarray.

3. Now the leftmost element of unsorted subarray becomes a part (rightmost) of sorted subarray and will not be a part of unsorted subarray.

## A selection sort works as follows:

This is our initial array A = [5, 2, 6, 7, 2, 1, 0, 3]

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

Leftmost element of unsorted part = A

Minimum element of unsorted part = A

We will swap A and A then, make A part of sorted subarray.

This is the final sorted array.

### Pseudocode

FindMinIndex:

```FindMinIndex(Arr[], start, end)
min_index = start

FOR i from (start + 1) to end:
IF Arr[i] < Arr[min_index]:
min_index = i
END of IF
END of FOR

Return min_index```

Suppose, there are ‘n’ elements in the array. Therefore, at worst case, there can be n iterations in FindMinIndex() for start = 1 and end = n. We did not take any auxiliary space.

Therefore,

Time complexity: O(n)

Space complexity: O(1)

Selection Sort:

```SelectionSort(Arr[], arr_size):
FOR i from 1 to arr_size:
min_index = FindMinIndex(Arr, i, arr_size)

IF i != min_index:
swap(Arr[i], Arr[min_index])
END of IF
END of FOR```

Suppose, there are ‘n’ elements in the array. Therefore, at worst case, there can be n iterations in FindMinIndex() for start = 1 and end = n. No auxiliary space used.

Total iterations = (n – 1) + (n – 2) + . . . + 1 = (n * (n – 1)) / 2 = (n2 – n) / 2

Therefore,

Time complexity: O(n2)

Space complexity: O(1)

### Implementation:

Following are C, C++, Java and Python implementations of Selection Sort.

### C C++ Java Python

Resource: Interviewbit.com

### Recent Posts

See All

#### 5 Ways To Loop Through An Array In PHP

PHP, just like most other programming languages has multiple ways to loop through arrays. The most popular ways to do it usually is with a while, for and foreach operator, but, believe it or not, ther