Selection Sort Algorithm in DSA Javascript - Time & Space Complexity
We want to know how the time taken by selection sort changes as the list gets bigger.
How does the number of steps grow when sorting more items?
Analyze the time complexity of the following code snippet.
function selectionSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
return arr;
}
This code sorts an array by repeatedly finding the smallest item and moving it to the front.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop compares elements to find the smallest item.
- How many times: The outer loop runs once for each element except the last, and the inner loop runs fewer times each pass but still many times overall.
As the list grows, the number of comparisons grows much faster because each item is compared with many others.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 comparisons |
| 100 | About 4,950 comparisons |
| 1000 | About 499,500 comparisons |
Pattern observation: The operations grow roughly by the square of the input size.
Time Complexity: O(n²)
This means if you double the number of items, the work roughly quadruples.
[X] Wrong: "Selection sort gets faster if the list is already sorted."
[OK] Correct: Selection sort always checks all pairs, so it takes the same time no matter the order.
Understanding selection sort's time helps you compare it with faster methods and shows you can analyze simple algorithms clearly.
"What if we stopped searching for the minimum after finding the first smaller element? How would the time complexity change?"
Practice
Selection Sort algorithm?Solution
Step 1: Understand Selection Sort process
Selection Sort works by finding the smallest element in the unsorted part and swapping it with the first unsorted element.Step 2: Compare with other sorting methods
Other options describe Merge Sort, Bubble Sort, and Quick Sort, which are different algorithms.Final Answer:
Repeatedly find the smallest element and move it to the front -> Option BQuick Check:
Selection Sort = Find smallest and swap [OK]
- Confusing Selection Sort with Bubble Sort
- Thinking it uses divide and conquer
- Assuming it uses a pivot element
arr[i] and arr[minIndex] in JavaScript during Selection Sort?Solution
Step 1: Identify JavaScript swap syntax
JavaScript supports swapping using destructuring assignment:[a, b] = [b, a].Step 2: Check each option
temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; is valid but missingletorvarfor temp and is verbose. arr[i], arr[minIndex] = arr[minIndex], arr[i]; is invalid syntax in JavaScript. swap(arr[i], arr[minIndex]); is a function call but no such built-in function exists.Final Answer:
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; -> Option CQuick Check:
Destructuring swap = [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; [OK]
- Using Python-style tuple swap syntax
- Calling undefined swap function
- Forgetting to declare temporary variable
[29, 10, 14, 37, 13]?Solution
Step 1: Perform Selection Sort step-by-step
Initial array: [29, 10, 14, 37, 13]
Find smallest from index 0 to end: 10 at index 1, swap with 29.
Array: [10, 29, 14, 37, 13]
Find smallest from index 1 to end: 13 at index 4, swap with 29.
Array: [10, 13, 14, 37, 29]
Find smallest from index 2 to end: 14 at index 2, no swap.
Find smallest from index 3 to end: 29 at index 4, swap with 37.
Array: [10, 13, 14, 29, 37]Step 2: Confirm final sorted array
The array is sorted in ascending order: [10, 13, 14, 29, 37].Final Answer:
[10, 13, 14, 29, 37] -> Option DQuick Check:
Sorted array = [10, 13, 14, 29, 37] [OK]
- Not swapping correctly after finding minimum
- Confusing index positions during swaps
- Assuming array is unchanged
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
arr[i] = arr[minIndex];
arr[minIndex] = arr[i];
}
return arr;
}Solution
Step 1: Analyze the swap logic
The swap is done by assigningarr[i] = arr[minIndex]first, thenarr[minIndex] = arr[i]. This overwritesarr[i]before assigning toarr[minIndex], causing both to have the same value.Step 2: Correct swap method
Use a temporary variable or destructuring assignment to swap without overwriting:let temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp;Final Answer:
Swapping lines overwrite values causing incorrect swaps -> Option AQuick Check:
Incorrect swap overwrites values = Swapping lines overwrite values causing incorrect swaps [OK]
- Swapping without temp variable
- Incorrect loop boundaries
- Not returning the array
[4, 2, 4, 3, 2], how many swaps will Selection Sort perform to fully sort it?Solution
Step 1: Trace Selection Sort swaps on the array
Initial array: [4, 2, 4, 3, 2]
Pass 1: smallest from index 0-4 is 2 at index 1, swap with 4 at index 0.
Array: [2, 4, 4, 3, 2] (1 swap)
Pass 2: smallest from index 1-4 is 2 at index 4, swap with 4 at index 1.
Array: [2, 2, 4, 3, 4] (2 swaps)
Pass 3: smallest from index 2-4 is 3 at index 3, swap with 4 at index 2.
Array: [2, 2, 3, 4, 4] (3 swaps)
Pass 4: smallest from index 3-4 is 4 at index 3, no swap.
Pass 5: last element, no swap.Step 2: Count total swaps
Total swaps performed = 3.Final Answer:
3 -> Option AQuick Check:
Count swaps during passes = 3 [OK]
- Counting all comparisons as swaps
- Ignoring swaps when minIndex equals current index
- Assuming duplicates reduce swaps to zero
Ready to prove it?
Pass the test and this pattern moves unknown
