Selection Sort

Selection Sort

Visualization

Algorithm Steps

  1. Start at the beginning of the array.
  2. Assume the first unsorted element is the smallest.
  3. Scan the remaining unsorted elements to find the smallest element.
  4. Swap the smallest element with the first unsorted element.
  5. Repeat the process for the rest of the array until it is sorted.

Selection Sort Program Code

Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        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

if __name__ == '__main__':
    arr = [6, 3, 8, 2, 7, 4]
    selection_sort(arr)
    print("Sorted array is:", arr)

Detailed Step by Step Example

Let us take the following array and apply the Selection Sort algorithm to sort the array in ascending order.

{ "array": [6,3,8,2,7,4], "showIndices": true, "specialIndices": [] }

Pass 1

Assume index 0 (6) is the minimum in the unsorted portion of array.

➜ Comparing current minimum 6 at index=0 with 3 at index=1

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndicesBorder": [0,1], "highlightIndicesGreen": [], "specialIndices": [], "labels": { "0": "min" } }

Found new minimum 3 at index=1.

➜ Comparing current minimum 3 at index=1 with 8 at index=2

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndicesBorder": [1,2], "highlightIndicesGreen": [], "specialIndices": [], "labels": { "1": "min" } }

No change. 8 is not smaller than current minimum.

➜ Comparing current minimum 3 at index=1 with 2 at index=3

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndicesBorder": [1,3], "highlightIndicesGreen": [], "specialIndices": [], "labels": { "1": "min" } }

Found new minimum 2 at index=3.

➜ Comparing current minimum 2 at index=3 with 7 at index=4

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndicesBorder": [3,4], "highlightIndicesGreen": [], "specialIndices": [], "labels": { "3": "min" } }

No change. 7 is not smaller than current minimum.

➜ Comparing current minimum 2 at index=3 with 4 at index=5

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndicesBorder": [3,5], "highlightIndicesGreen": [], "specialIndices": [], "labels": { "3": "min" } }

No change. 4 is not smaller than current minimum.

➜ Minimum in the unsorted portion of array is 2 at index=3.

{ "array": [6,3,8,2,7,4], "showIndices": true, "highlightIndicesBorder": [3], "highlightIndicesGreen": [], "specialIndices": [] }

Swapping 6 and 2 to place the smallest element at correct position.

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndices": [0,3], "highlightIndicesGreen": [0], "specialIndices": [] }

Element 2 is now at its correct position.

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesGreen": [0], "specialIndices": [] }

Pass 2

Assume index 1 (3) is the minimum in the unsorted portion of array.

➜ Comparing current minimum 3 at index=1 with 8 at index=2

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [1,2], "highlightIndicesGreen": [0], "specialIndices": [], "labels": { "1": "min" } }

No change. 8 is not smaller than current minimum.

➜ Comparing current minimum 3 at index=1 with 6 at index=3

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [1,3], "highlightIndicesGreen": [0], "specialIndices": [], "labels": { "1": "min" } }

No change. 6 is not smaller than current minimum.

➜ Comparing current minimum 3 at index=1 with 7 at index=4

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [1,4], "highlightIndicesGreen": [0], "specialIndices": [], "labels": { "1": "min" } }

No change. 7 is not smaller than current minimum.

➜ Comparing current minimum 3 at index=1 with 4 at index=5

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [1,5], "highlightIndicesGreen": [0], "specialIndices": [], "labels": { "1": "min" } }

No change. 4 is not smaller than current minimum.

➜ Minimum in the unsorted portion of array is 3 at index=1.

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [1], "highlightIndicesGreen": [0], "specialIndices": [] }

No swap needed. Minimum element is already at the correct position.

Element 3 is now at its correct position.

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesGreen": [0,1], "specialIndices": [] }

Pass 3

Assume index 2 (8) is the minimum in the unsorted portion of array.

➜ Comparing current minimum 8 at index=2 with 6 at index=3

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [2,3], "highlightIndicesGreen": [0,1], "specialIndices": [], "labels": { "2": "min" } }

Found new minimum 6 at index=3.

➜ Comparing current minimum 6 at index=3 with 7 at index=4

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [3,4], "highlightIndicesGreen": [0,1], "specialIndices": [], "labels": { "3": "min" } }

No change. 7 is not smaller than current minimum.

➜ Comparing current minimum 6 at index=3 with 4 at index=5

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [3,5], "highlightIndicesGreen": [0,1], "specialIndices": [], "labels": { "3": "min" } }

Found new minimum 4 at index=5.

➜ Minimum in the unsorted portion of array is 4 at index=5.

{ "array": [2,3,8,6,7,4], "showIndices": true, "highlightIndicesBorder": [5], "highlightIndicesGreen": [0,1], "specialIndices": [] }

Swapping 8 and 4 to place the smallest element at correct position.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndices": [2,5], "highlightIndicesGreen": [0,1,2], "specialIndices": [] }

Element 4 is now at its correct position.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesGreen": [0,1,2], "specialIndices": [] }

Pass 4

Assume index 3 (6) is the minimum in the unsorted portion of array.

➜ Comparing current minimum 6 at index=3 with 7 at index=4

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesBorder": [3,4], "highlightIndicesGreen": [0,1,2], "specialIndices": [], "labels": { "3": "min" } }

No change. 7 is not smaller than current minimum.

➜ Comparing current minimum 6 at index=3 with 8 at index=5

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesBorder": [3,5], "highlightIndicesGreen": [0,1,2], "specialIndices": [], "labels": { "3": "min" } }

No change. 8 is not smaller than current minimum.

➜ Minimum in the unsorted portion of array is 6 at index=3.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesBorder": [3], "highlightIndicesGreen": [0,1,2], "specialIndices": [] }

No swap needed. Minimum element is already at the correct position.

Element 6 is now at its correct position.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesGreen": [0,1,2,3], "specialIndices": [] }

Pass 5

Assume index 4 (7) is the minimum in the unsorted portion of array.

➜ Comparing current minimum 7 at index=4 with 8 at index=5

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesBorder": [4,5], "highlightIndicesGreen": [0,1,2,3], "specialIndices": [], "labels": { "4": "min" } }

No change. 8 is not smaller than current minimum.

➜ Minimum in the unsorted portion of array is 7 at index=4.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesBorder": [4], "highlightIndicesGreen": [0,1,2,3], "specialIndices": [] }

No swap needed. Minimum element is already at the correct position.

Element 7 is now at its correct position.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesGreen": [0,1,2,3,4], "specialIndices": [] }

Array is fully sorted.

{ "array": [2,3,4,6,7,8], "showIndices": true, "highlightIndicesGreen": [0,1,2,3,4,5], "specialIndices": [] }