Selection
Last updated
Last updated
Selection Sort is another simple sorting algorithm that repeatedly finds the smallest element in an unsorted portion of an array and moves it to the beginning of the sorted portion of the array.
Here are the basic steps of the Selection Sort algorithm:
Starting with the first element in the array, search for the smallest element in the unsorted portion of the array.
Swap the smallest element found in step 1 with the first element in the unsorted portion of the array, effectively moving the smallest element to the beginning of the sorted portion of the array.
Repeat steps 1 and 2 for the remaining unsorted elements in the array until the entire array is sorted.
The array is now sorted. Selection Sort has a time complexity of O(n^2), making it relatively slow for large datasets.
However, it is easy to understand and implement, and can be useful for small datasets or as a starting point for more optimized sorting algorithms.
Implementation in JavaScript
Output