Notes
The selection sort selects the smallest number from the unsorted list of number and append it to the sort list; it repeats until no more number in the unsorted list.
Example (Using Two Lists)
Unsorted: 6, 8, 5, 4, 1, 3 Look for the smallest number in the unsorted list, in this case 1.
Sorted:
Unsorted: 6, 8, 5, 4, 3 Append it to the end of the sorted list.
Sorted: 1
Unsorted: 6, 8, 5, 4, 3 Again, look for the smallest number in the unsorted list, in this case 3.
Sorted: 1
Unsorted: 6, 8, 5, 4 Append it to the end of the sorted list.
Sorted: 1, 3
Unsorted: 6, 8, 5, 4 Again, look for the smallest number in the unsorted list, in this case 4.
Sorted: 1, 3
Unsorted: 6, 8, 5 Append it to the end of the sorted list.
Sorted: 1, 3, 4
Unsorted: 6, 8, 5 Again, look for the smallest number in the unsorted list, in this case 5.
Sorted: 1, 3, 4
Unsorted: 6, 8 Append it to the end of the sorted list.
Sorted: 1, 3, 4, 5
Unsorted: 6, 8 Again, look for the smallest number in the unsorted list, in this case 6.
Sorted: 1, 3, 4, 5
Unsorted: 8 Append it to the end of the sorted list.
Sorted: 1, 3, 4, 5, 6
Unsorted: 8 Again, look for the smallest number in the unsorted list, in this case 8.
Sorted: 1, 3, 4, 5, 6
Unsorted: Append it to the end of the sorted list.
Sorted: 1, 3, 4, 5, 6, 8
Example (Using One List)
We do not really need two list to use selection sort, we can just use one list, but divide the list into two sections. One section is the sorted (left side), the other section is the unsorted (right side). We will use the " | " symbol to show the division between the sorted and unsorted section.
| 6, 8, 5, 4, 1, 3 Look for the smallest number in the unsorted list, in this case 1.
1 | 8, 5, 4, 6, 3 Exchange the smallest number with the 1st number.
1 | 8, 5, 4, 6, 3 Again, look for the smallest number in the unsorted list, in this case 3.
1, 3 | 5, 4, 6, 8 Exchange the smallest number with the 2nd number.
1, 3 | 5, 4, 6, 8 Again, look for the smallest number in the unsorted list, in this case 4.
1, 3, 4 | 5, 6, 8 Exchange the smallest number with the 3rd number.
1, 3, 4 | 5, 6, 8 Again, look for the smallest number in the unsorted list, in this case 5.
1, 3, 4, 5 | 6, 8 Since the number is already in the correct place, no exchange is needed (or exchange with itself).
1, 3, 4, 5 | 6, 8 Again, look for the smallest number in the unsorted list, in this case 6.
1, 3, 4, 5, 6 | 8 Since the number is already in the correct place, no exchange is needed (or exchange with itself).
1, 3, 4, 5, 6 | 8 Again, look for the smallest number in the unsorted list, in this case 8.
1, 3, 4, 5, 6, 8 | Since the number is already in the correct place, no exchange is needed (or exchange with itself).
Sample Code in Python
#Sorting the list [6,8,5,4,1,3] in ascending order
list1 = [6,8,5,4,1,3]
def selection_sort(sort_list):
list_length = len(sort_list)
sorted_index = 0
for i in range(list_length):
smallest_num = sort_list[i]
smallest_index = i;
for j in range(i, list_length):
if list1[j]<smallest_num:
smallest_num = sort_list[j]
smallest_index = j
if smallest_index != i:
temp = list1[sorted_index]
list1[sorted_index] = list1[smallest_index]
list1[smallest_index] = temp
sorted_index += 1
selection_sort(list1)
print(list1)