0
0
DSA Javascriptprogramming~20 mins

Sorting Stability and When to Use Which Sort in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Stability 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 this stable sort on objects?
Consider the array of objects sorted by the 'value' key using a stable sort. What is the output after sorting?
DSA Javascript
const arr = [
  {id: 1, value: 3},
  {id: 2, value: 1},
  {id: 3, value: 3},
  {id: 4, value: 2}
];

const sorted = arr.slice().sort((a, b) => a.value - b.value);
console.log(sorted.map(o => o.id));
A[1, 3, 4, 2]
B[2, 4, 3, 1]
C[2, 4, 1, 3]
D[4, 2, 1, 3]
Attempts:
2 left
💡 Hint
Stable sort keeps the original order of equal elements.
🧠 Conceptual
intermediate
1:30remaining
Which sorting algorithm is stable by default?
Among the following sorting algorithms, which one is stable by default?
AQuick Sort
BMerge Sort
CHeap Sort
DSelection Sort
Attempts:
2 left
💡 Hint
Think about which algorithm merges sorted sublists without changing order of equal elements.
Predict Output
advanced
2:30remaining
What error or output does this unstable sort produce?
Given this array of objects sorted by an unstable algorithm, what is the output of the ids after sorting by 'value'?
DSA Javascript
const arr = [
  {id: 'a', value: 5},
  {id: 'b', value: 3},
  {id: 'c', value: 5},
  {id: 'd', value: 3}
];

// Simulate unstable sort by swapping equal elements
function unstableSort(array) {
  array.sort((x, y) => x.value - y.value);
  // Swap equal elements to simulate instability
  for (let i = 0; i < array.length - 1; i++) {
    if (array[i].value === array[i + 1].value) {
      [array[i], array[i + 1]] = [array[i + 1], array[i]];
      i++;
    }
  }
  return array;
}

const sorted = unstableSort(arr);
console.log(sorted.map(o => o.id));
A['d', 'b', 'c', 'a']
B['b', 'd', 'a', 'c']
C['b', 'd', 'c', 'a']
D['a', 'c', 'b', 'd']
Attempts:
2 left
💡 Hint
The unstable sort swaps equal elements, changing their original order.
🚀 Application
advanced
1:30remaining
When should you prefer a stable sort over an unstable sort?
Choose the best scenario where using a stable sort is important.
ASorting a list of employees first by department, then by joining date
BSorting numbers where duplicates do not matter
CSorting a list of unique IDs in any order
DSorting a small array where speed is the only concern
Attempts:
2 left
💡 Hint
Stable sort preserves order of equal keys, useful for multi-level sorting.
🧠 Conceptual
expert
2:00remaining
Which sorting algorithm is NOT stable and why?
Among the following, which sorting algorithm is NOT stable and what is the main reason?
AMerge Sort - because it merges sorted sublists
BBubble Sort - because it compares and swaps adjacent elements
CInsertion Sort - because it swaps adjacent elements only
DHeap Sort - because it rearranges elements using a heap structure
Attempts:
2 left
💡 Hint
Think about which algorithm changes relative order by moving elements far apart.