Selection sort is a simple comparison-based sorting algorithm. It works by dividing the input array into two parts: the sublist of items already sorted, and the sublist of items remaining to be sorted. The algorithm repeatedly selects the smallest element from the unsorted sublist and swaps it with the element at the beginning of the unsorted sublist. This process continues until the entire array is sorted.
How Selection Sort Works
Here’s a step-by-step breakdown of how the selection sort algorithm works:
- Find the minimum element in the unsorted sublist.
- Swap this minimum element with the first element of the unsorted sublist.
- Move the boundary of the sorted sublist one element to the right.
- Repeat steps 1-3 until the entire array is sorted.
Example Code in Python
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
# Find the index of the minimum element in the remaining unsorted sublist
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
# Swap the minimum element with the first element of the unsorted sublist
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr
# Test the selection_sort function
arr = [64, 25, 12, 22, 11]
sorted_arr = selection_sort(arr)
print("Sorted array:", sorted_arr)
Time Complexity
The time complexity of the selection sort algorithm is O(n^2), where n is the number of elements in the array. This is because, in the worst case, the algorithm performs approximately n^2/2 comparisons and swaps.
Conclusion
Selection sort is a simple yet not very efficient sorting algorithm. Its main advantage is that it requires only a constant amount of additional space (in-place sorting). However, for larger arrays, other sorting algorithms such as merge sort or quicksort are generally preferred due to their better time complexity. #algorithm #python