⬅ Previous Topic
Bubble SortNext Topic ⮕
Insertion Sort⬅ Previous Topic
Bubble SortNext Topic ⮕
Insertion SortTopic Contents
Given an array of integers, your task is to sort the array in ascending order using the Selection Sort algorithm.
Selection Sort works by repeatedly selecting the smallest element from the unsorted portion of the array and moving it to the correct position in the sorted portion.
The algorithm is in-place and does not require extra space, but it is not the most efficient for large datasets.
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)
Let us take the following array and apply the Selection Sort algorithm to sort the array in ascending order.
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.
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.
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.
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.
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.
⬅ Previous Topic
Bubble SortNext Topic ⮕
Insertion SortYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.