Selection Sort
Selection Sort
0 1 2 3 4
arr 105 120 10 200 20
Implementing Selection Sort Algorithm (Contd.)
Pass 1
n=5
Search the minimum value in the array, arr[0] to arr[n – 1].
0 1 2 3 4
arr 105 120 10 200 20
min
Implementing Selection Sort Algorithm (Contd.)
Pass 1
n=5
Search the minimum value in the array, arr[0] to arr[n – 1].
Swap the minimum value with the value at index 0.
Swap
0 1 2 3 4
arr 10
10 120 10
10 200 20
min
Implementing Selection Sort Algorithm (Contd.)
Pass 1
n=5
Search the minimum value in the array, arr[0] to arr[n – 1].
Swap the minimum value with the value at index 0.
0 1 2 3 4
arr 10 120 105 200 20
Pass 2
n=5
Search the minimum value in the array, arr[1] to arr[n – 1].
Swap the minimum value with the value at index 1.
Swap
0 1 2 3 4
arr 20 105 200 120
10 120 20
min
Implementing Selection Sort Algorithm (Contd.)
Pass 2
n=5
Search the minimum value in the array, arr[1] to arr[n – 1].
Swap the minimum value with the value at index 1.
0 1 2 3 4
arr 10 120
20 10
105200
200 120
2
Pass 3
n=5
Search the minimum value in the array, arr[2] to arr[n – 1].
Swap the minimum value with the value at index 2.
0 1 2 3 4
arr 10 120
20 10
105 200
20 120
2
min
Implementing Selection Sort Algorithm (Contd.)
Pass 3
n=5
Search the minimum value in the array, arr[2] to arr[n – 1].
Swap the minimum value with the value at index 2.
0 1 2 3 4
arr 10 120
20 10
105 200
20 120
2
min
The third smallest value is placed at its correct location after Pass 3
Implementing Selection Sort Algorithm (Contd.)
Pass 4
n=5
Search the minimum value in the array, arr[3] to arr[n – 1].
Swap the minimum value with the value at index 3.
Swap
0 1 2 3 4
arr 10 120
20 10
105 12
20 120
20
2
min
Implementing Selection Sort Algorithm (Contd.)
Pass 4
n=5
Search the minimum value in the array, arr[3] to arr[n – 1].
Swap the minimum value with the value at index 3.
0 1 2 3 4
arr 10 120
20 10 20 200
105 120 2
The fourth smallest value is placed at its correct location after Pass 4
Implementing Selection Sort Algorithm (Contd.)
Pass 4
n=5
The list is now sorted.
0 1 2 3 4
arr 10 120
20 10 20 200
105 120 2
Implementing Selection Sort Algorithm (Contd.)