0
0
DSA Javascriptprogramming

Selection Sort Algorithm in DSA Javascript

Choose your learning style9 modes available
Mental Model
Selection sort finds the smallest item and puts it at the start, then repeats for the rest.
Analogy: Imagine picking the smallest apple from a basket and placing it in a new row, then picking the next smallest from the remaining apples, and so on.
Array: [7, 3, 5, 2, 4]
Indexes:  0  1  2  3  4

Start with first element (↑ at index 0)
Dry Run Walkthrough
Input: array: [7, 3, 5, 2, 4]
Goal: Sort the array in ascending order using selection sort
Step 1: Find smallest from index 0 to end; swap with index 0
[2, 3, 5, 7, 4]
Indexes:  0  1  2  3  4
↑ at index 0
Why: We put the smallest number (2) at the start to begin sorting
Step 2: Find smallest from index 1 to end; swap with index 1
[2, 3, 5, 7, 4]
Indexes:  0  1  2  3  4
↑ at index 1
Why: Next smallest (3) is already at index 1, so no swap needed
Step 3: Find smallest from index 2 to end; swap with index 2
[2, 3, 4, 7, 5]
Indexes:  0  1  2  3  4
↑ at index 2
Why: Smallest from remaining is 4, swap it with 5 at index 2
Step 4: Find smallest from index 3 to end; swap with index 3
[2, 3, 4, 5, 7]
Indexes:  0  1  2  3  4
↑ at index 3
Why: Smallest from last two is 5, swap with 7 at index 3
Step 5: Only one element left at index 4, no action needed
[2, 3, 4, 5, 7]
Indexes:  0  1  2  3  4
Sorting complete
Why: Last element is in place, array is sorted
Result:
[2, 3, 4, 5, 7]
Annotated Code
DSA Javascript
class SelectionSort {
  static sort(arr) {
    const n = arr.length;
    for (let i = 0; i < n - 1; i++) {
      let minIndex = i;
      // find index of smallest element in unsorted part
      for (let j = i + 1; j < n; j++) {
        if (arr[j] < arr[minIndex]) {
          minIndex = j;
        }
      }
      // swap smallest found with element at i
      if (minIndex !== i) {
        [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
      }
    }
    return arr;
  }
}

// Driver code
const inputArray = [7, 3, 5, 2, 4];
const sortedArray = SelectionSort.sort(inputArray);
console.log(sortedArray.join(", ") + "\n");
for (let i = 0; i < n - 1; i++) {
iterate over each position to place the smallest element
let minIndex = i;
assume current position holds smallest element
for (let j = i + 1; j < n; j++) {
search the rest of the array for a smaller element
if (arr[j] < arr[minIndex]) { minIndex = j; }
update minIndex when a smaller element is found
if (minIndex !== i) { [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; }
swap the smallest element found with current position
OutputSuccess
2, 3, 4, 5, 7
Complexity Analysis
Time: O(n^2) because for each element we scan the rest to find the smallest
Space: O(1) because sorting is done in place without extra storage
vs Alternative: Compared to bubble sort, selection sort does fewer swaps but still has O(n^2) comparisons
Edge Cases
empty array
returns empty array immediately without error
DSA Javascript
for (let i = 0; i < n - 1; i++) {
array with one element
returns the same single-element array unchanged
DSA Javascript
for (let i = 0; i < n - 1; i++) {
array with all elements equal
no swaps occur, array remains unchanged
DSA Javascript
if (minIndex !== i) { [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; }
When to Use This Pattern
When you need a simple, easy-to-understand sorting method and can afford O(n^2) time, selection sort is a good first choice.
Common Mistakes
Mistake: Swapping elements inside the inner loop instead of after finding the minimum
Fix: Only swap once after the inner loop finishes to place the smallest element
Mistake: Not updating minIndex when a smaller element is found
Fix: Update minIndex inside the inner loop whenever a smaller element is found
Summary
Selection sort repeatedly finds the smallest element and moves it to the front.
Use it when you want a simple sorting method and the list is small or nearly sorted.
The key insight is to find the smallest element in the unsorted part and swap it once per iteration.