0
0
DSA Javascriptprogramming~20 mins

Selection Sort Algorithm in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Selection Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of selection sort on this array?
Given the array [29, 10, 14, 37, 13], what will be the array after the first two passes of selection sort?
DSA Javascript
const arr = [29, 10, 14, 37, 13];

// After first pass
// After second pass

// What is arr now?
A[10, 14, 13, 29, 37]
B[10, 13, 14, 29, 37]
C[10, 14, 13, 37, 29]
D[10, 13, 14, 37, 29]
Attempts:
2 left
💡 Hint

Selection sort picks the smallest element and swaps it with the current position.

🧠 Conceptual
intermediate
1:30remaining
How many swaps does selection sort perform on an array of length n?
Consider selection sort on an array of length n. How many swaps does it perform in the worst case?
A0 swaps
Bn swaps
Cn*(n-1)/2 swaps
Dn-1 swaps
Attempts:
2 left
💡 Hint

Selection sort swaps only once per pass after finding the minimum.

Predict Output
advanced
2:30remaining
What is the final sorted array after running this selection sort code?
Given the code below, what is the final printed array?
DSA Javascript
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;
      }
    }
    if (minIndex !== i) {
      [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
    }
  }
  return arr;
}

const result = selectionSort([64, 25, 12, 22, 11]);
console.log(result);
A[11, 12, 22, 25, 64]
B[64, 25, 12, 22, 11]
C[11, 22, 12, 25, 64]
D[12, 11, 22, 25, 64]
Attempts:
2 left
💡 Hint

Selection sort sorts the array in ascending order by repeatedly selecting the smallest element.

🔧 Debug
advanced
2:00remaining
What error does this selection sort code produce?
Consider this code snippet. What error will it raise when run?
DSA Javascript
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;
}

selectionSort([3, 1, 2]);
ATypeError: Cannot destructure property of undefined
BRangeError: Maximum call stack size exceeded
CTypeError: Cannot read property 'length' of undefined
Dundefined is not a function
Attempts:
2 left
💡 Hint

Check the loop condition for i and how it affects array access.

🧠 Conceptual
expert
1:30remaining
What is the time complexity of selection sort and why?
Select the correct time complexity of selection sort and the reason behind it.
AO(n) because it only swaps elements once per pass
BO(n log n) because it divides the array and merges sorted parts
CO(n^2) because it uses nested loops to find the minimum element for each position
DO(log n) because it uses binary search to find the minimum
Attempts:
2 left
💡 Hint

Think about how many comparisons selection sort makes in total.