DEV Community

Ajith R
Ajith R

Posted on

Selection sort that you dont want to know

Selection sort is a simple, in-place comparison-based sorting algorithm that divides the input array into two parts: a sorted subarray and an unsorted subarray. It repeatedly selects the smallest (or largest) element from the unsorted subarray and swaps it with the first element of the unsorted subarray, thereby expanding the sorted subarray by one element.

Algorithm:

  1. Selection: Find the minimum (or maximum) element in the unsorted subarray.
  2. Swap: Swap the minimum element with the first element of the unsorted subarray.
  3. Repeat: Repeat steps 1 and 2 for the remaining unsorted subarray, moving the boundary of the sorted subarray one element to the right.

JavaScript Implementation:

function selectionSort(arr) {
    const n = arr.length;
    for (let i = 0; i < n - 1; i++) {
        let minIndex = i;
        for (let j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        if (minIndex !== i) {
            [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; // Swap elements
        }
    }
    return arr;
}
Enter fullscreen mode Exit fullscreen mode

Advantages of Selection Sort:

  1. Simplicity: Selection sort is straightforward to understand and implement. It involves simple logic and requires only a few lines of code.

  2. In-Place Sorting: Selection sort operates in-place, meaning it does not require additional memory beyond the input array. This makes it memory efficient and suitable for sorting large arrays.

  3. Stable Sorting: Selection sort is stable, meaning it preserves the relative order of equal elements. This property may be desirable in certain applications where maintaining the original order is important.

Disadvantages of Selection Sort:

  1. Inefficiency: Selection sort has poor time complexity compared to more efficient sorting algorithms such as quicksort or merge sort. Its average and worst-case time complexity are both O(n^2), making it inefficient for sorting large arrays.

  2. Lack of Adaptability: Selection sort's performance is not affected by the initial order of elements in the array. It always performs the same number of comparisons and swaps regardless of input characteristics.

  3. Unsuitability for Large Datasets: Selection sort's inefficiency makes it unsuitable for sorting large datasets. It is primarily used for educational purposes or sorting small arrays where simplicity is more important than efficiency.

Conclusion:

https://github.com/ajithr116/Data-Structures/tree/main/06-sorting/selectionsort is a simple and intuitive sorting algorithm suitable for educational purposes or sorting small datasets. While it has advantages in simplicity and stability, its inefficiency and poor time complexity make it less practical for real-world applications requiring sorting of large arrays. In most cases, more efficient sorting algorithms are preferred for sorting tasks.


Top comments (0)