Selection sort
Speed
About algorithm
Explanation
Selection Sort repeatedly finds the minimum element from the unsorted part and moves it to the beginning. It's simple but inefficient for large datasets.
Steps
- Start from the first element.
- Find the smallest element in the unsorted part of the array.
- Swap it with the current element.
- Move the boundary of the sorted part one step forward.
- Repeat until the array is sorted.
Pseudocode
selectionSort(arr): n = len(arr) for i from 0 to n - 1: minIndex = i for j from i + 1 to n - 1: if arr[j] < arr[minIndex]: minIndex = j swap arr[i] with arr[minIndex]
Complexity
Time: O(n²)
Space: O(1)