Overview
Selection Sort is a simple comparison-based sorting algorithm. It divides the
list into a sorted and an unsorted part. The algorithm repeatedly selects the
smallest (or largest, depending on the order) element from the unsorted part
and swaps it with the first element of the unsorted part, effectively growing
the sorted part of the list one element at a time.
Time Complexity:
Best case: O(n²) - The algorithm always performs n-1 comparisons for
each element, even if the list is already sorted.
Average case: O(n²) - The number of comparisons remains constant for any
input order, making it inefficient for large datasets.
Worst case: O(n²) - As with the best and
average cases, the number of comparisons is always the same.
Space Complexity: O(1) - It is an in-place sorting algorithm.
for i = 0 to length(list) - 1 minIndex = i for j = i + 1 to length(list) - 1 if list[j] < list[minIndex] minIndex = j end for if minIndex ≠ i swap list[i] and list[minIndex]SHORT EXPLANATION------------------1. Start from the first element (index 0)2. Find the smallest element in the remaining list3. Swap the smallest element with the first element4. Move to the next element and repeat the process5. Continue until the entire list is sorted6. The algorithm ensures that the left portion of the list is always sorted