Algorithm Steps
- Start at the beginning of the array.
- Assume the first unsorted element is the smallest.
- Scan the remaining unsorted elements to find the smallest element.
- Swap the smallest element with the first unsorted element.
- Repeat the process for the rest of the array until it is sorted.
Selection Sort Program Code
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.
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
Found new minimum 3
at index=1.
➜ Comparing current minimum 3
at index=1 with 8
at index=2
No change. 8
is not smaller than current minimum.
➜ Comparing current minimum 3
at index=1 with 2
at index=3
Found new minimum 2
at index=3.
➜ Comparing current minimum 2
at index=3 with 7
at index=4
No change. 7
is not smaller than current minimum.
➜ Comparing current minimum 2
at index=3 with 4
at index=5
No change. 4
is not smaller than current minimum.
➜ Minimum in the unsorted portion of array is 2
at index=3.
Swapping 6
and 2
to place the smallest element at correct position.
Element 2
is now at its correct position.
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
No change. 8
is not smaller than current minimum.
➜ Comparing current minimum 3
at index=1 with 6
at index=3
No change. 6
is not smaller than current minimum.
➜ Comparing current minimum 3
at index=1 with 7
at index=4
No change. 7
is not smaller than current minimum.
➜ Comparing current minimum 3
at index=1 with 4
at index=5
No change. 4
is not smaller than current minimum.
➜ Minimum in the unsorted portion of array is 3
at index=1.
No swap needed. Minimum element is already at the correct position.
Element 3
is now at its correct position.
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
Found new minimum 6
at index=3.
➜ Comparing current minimum 6
at index=3 with 7
at index=4
No change. 7
is not smaller than current minimum.
➜ Comparing current minimum 6
at index=3 with 4
at index=5
Found new minimum 4
at index=5.
➜ Minimum in the unsorted portion of array is 4
at index=5.
Swapping 8
and 4
to place the smallest element at correct position.
Element 4
is now at its correct position.
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
No change. 7
is not smaller than current minimum.
➜ Comparing current minimum 6
at index=3 with 8
at index=5
No change. 8
is not smaller than current minimum.
➜ Minimum in the unsorted portion of array is 6
at index=3.
No swap needed. Minimum element is already at the correct position.
Element 6
is now at its correct position.
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
No change. 8
is not smaller than current minimum.
➜ Minimum in the unsorted portion of array is 7
at index=4.
No swap needed. Minimum element is already at the correct position.
Element 7
is now at its correct position.
Array is fully sorted.