Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Comparison Based vs Non Comparison Based Sorting in DSA Javascript - Compare & Choose

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Comparison Based Sorting Algorithm
What is the output of the following JavaScript code that uses a comparison based sorting method?
DSA Javascript
const arr = [5, 3, 8, 1, 2];
arr.sort((a, b) => a - b);
console.log(arr.join(' -> ') + ' -> null');
A1 -> 3 -> 2 -> 5 -> 8 -> null
B5 -> 3 -> 8 -> 1 -> 2 -> null
C8 -> 5 -> 3 -> 2 -> 1 -> null
D1 -> 2 -> 3 -> 5 -> 8 -> null
Attempts:
2 left
💡 Hint
Remember that the sort method with (a, b) => a - b sorts numbers in ascending order.
❓ Predict Output
intermediate
2:00remaining
Output of Non Comparison Based Sorting Algorithm
What is the output of the following JavaScript code that uses a non comparison based sorting method (counting sort) on a small array?
DSA Javascript
function countingSort(arr) {
  const max = Math.max(...arr);
  const count = new Array(max + 1).fill(0);
  for (const num of arr) count[num]++;
  const sorted = [];
  for (let i = 0; i <= max; i++) {
    while (count[i] > 0) {
      sorted.push(i);
      count[i]--;
    }
  }
  return sorted;
}
const arr = [4, 2, 2, 8, 3, 3, 1];
const sortedArr = countingSort(arr);
console.log(sortedArr.join(' -> ') + ' -> null');
A1 -> 2 -> 2 -> 3 -> 3 -> 4 -> 8 -> null
B8 -> 4 -> 3 -> 3 -> 2 -> 2 -> 1 -> null
C1 -> 3 -> 2 -> 4 -> 8 -> 2 -> 3 -> null
D2 -> 2 -> 3 -> 3 -> 4 -> 8 -> 1 -> null
Attempts:
2 left
💡 Hint
Counting sort counts occurrences and outputs sorted values in order.
🧠 Conceptual
advanced
2:00remaining
Time Complexity Difference
Which statement correctly describes the time complexity difference between comparison based and non comparison based sorting algorithms?
ANon comparison based sorting algorithms can achieve linear time O(n) under certain conditions, while comparison based sorting algorithms cannot do better than O(n log n).
BBoth comparison based and non comparison based sorting algorithms always run in O(n log n) time regardless of input.
CComparison based sorting algorithms have a best time complexity of O(n), while non comparison based sorting algorithms always run in O(n log n).
DComparison based sorting algorithms are always faster than non comparison based sorting algorithms.
Attempts:
2 left
💡 Hint
Think about the theoretical lower bound for comparison based sorting.
❓ Predict Output
advanced
2:00remaining
Output of Non Comparison Based Sorting Algorithm (Radix Sort)
What is the output of the following JavaScript code that uses a non comparison based sorting method (radix sort)?
DSA Javascript
function radixSort(arr) {
  const max = Math.max(...arr);
  let exp = 1;
  while (Math.floor(max / exp) > 0) {
    const output = new Array(arr.length).fill(0);
    const count = new Array(10).fill(0);
    for (let i = 0; i < arr.length; i++) {
      const index = Math.floor(arr[i] / exp) % 10;
      count[index]++;
    }
    for (let i = 1; i < 10; i++) {
      count[i] += count[i - 1];
    }
    for (let i = arr.length - 1; i >= 0; i--) {
      const index = Math.floor(arr[i] / exp) % 10;
      output[count[index] - 1] = arr[i];
      count[index]--;
    }
    arr = output;
    exp *= 10;
  }
  return arr;
}
const arr = [170, 45, 75, 90, 802, 24, 2, 66];
console.log(radixSort(arr).join(' -> ') + ' -> null');
ATypeError: Assignment to constant variable.
BOutputs sorted array correctly: 2 -> 24 -> 45 -> 66 -> 75 -> 90 -> 170 -> 802 -> null
CReferenceError: arr is not defined.
DRangeError: Maximum call stack size exceeded.
Attempts:
2 left
💡 Hint
This is a correct implementation of LSD radix sort using counting sort per digit.
🚀 Application
expert
2:00remaining
Choosing Sorting Algorithm for Large Dataset with Small Range
You have a large dataset of 10 million integers, but all values are between 0 and 1000. Which sorting algorithm is the best choice for fastest sorting?
AMergeSort (comparison based) because it guarantees O(n log n) time and is stable.
BQuickSort (comparison based) because it has average O(n log n) time and works well on large datasets.
CCounting Sort (non comparison based) because it can sort in O(n) time when the range is small.
DBubble Sort (comparison based) because it is simple and easy to implement.
Attempts:
2 left
💡 Hint
Consider the input range and how it affects non comparison based sorting.

Practice

(1/5)
1. Which of the following sorting algorithms is non-comparison based?
easy
A. Heap Sort
B. Merge Sort
C. Quick Sort
D. Counting Sort

Solution

  1. Step 1: Understand comparison based sorting

    Comparison based sorting algorithms decide order by comparing elements, like Merge Sort, Quick Sort, and Heap Sort.
  2. Step 2: Identify non-comparison based sorting

    Counting Sort uses counting of element frequencies instead of comparisons, so it is non-comparison based.
  3. Final Answer:

    Counting Sort -> Option D
  4. Quick Check:

    Non-comparison based = Counting Sort [OK]
Hint: Counting Sort uses counts, no element comparisons [OK]
Common Mistakes:
  • Confusing Quick Sort as non-comparison
  • Thinking Merge Sort is non-comparison
  • Assuming Heap Sort is non-comparison
2. Which of the following is the correct syntax to call a non-comparison based sorting function countingSort(arr) in JavaScript?
easy
A. countingSort(arr);
B. countingSort arr;
C. countingSort{arr};
D. countingSort[arr];

Solution

  1. Step 1: Recall JavaScript function call syntax

    Functions are called with parentheses enclosing arguments, like func(args).
  2. Step 2: Match syntax with options

    Only countingSort(arr); uses correct parentheses syntax for function call.
  3. Final Answer:

    countingSort(arr); -> Option A
  4. Quick Check:

    Function call syntax = countingSort(arr); [OK]
Hint: Use parentheses () to call functions in JavaScript [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses
  • Missing parentheses around arguments
  • Using curly braces {} incorrectly
3. What will be the output of the following JavaScript code?
function countingSort(arr) {
  const max = Math.max(...arr);
  const count = new Array(max + 1).fill(0);
  arr.forEach(num => count[num]++);
  let sorted = [];
  count.forEach((c, num) => {
    for (let i = 0; i < c; i++) sorted.push(num);
  });
  return sorted;
}

const arr = [3, 1, 4, 1, 5];
console.log(countingSort(arr));
medium
A. [1, 1, 3, 4, 5]
B. [3, 1, 4, 1, 5]
C. [5, 4, 3, 1, 1]
D. [1, 3, 4, 5]

Solution

  1. Step 1: Count frequencies of each number

    Input array is [3,1,4,1,5]. Counting frequencies: 1 appears 2 times, 3 once, 4 once, 5 once.
  2. Step 2: Build sorted array from counts

    Using counts, output array is [1,1,3,4,5] in ascending order.
  3. Final Answer:

    [1, 1, 3, 4, 5] -> Option A
  4. Quick Check:

    Counting Sort output = sorted ascending array [OK]
Hint: Counting Sort outputs sorted array by frequency counts [OK]
Common Mistakes:
  • Confusing input and output arrays
  • Assuming original order is preserved
  • Thinking output is reversed
4. The following code attempts to implement Counting Sort but has a bug. What is the bug?
function countingSort(arr) {
  const max = Math.max(...arr);
  const count = new Array(max).fill(0);
  arr.forEach(num => count[num]++);
  let sorted = [];
  count.forEach((c, num) => {
    for (let i = 0; i < c; i++) sorted.push(num);
  });
  return sorted;
}
medium
A. Should use Math.min instead of Math.max
B. Array count size should be max + 1
C. Should initialize count with zeros
D. Should not use forEach on count array

Solution

  1. Step 1: Check count array size

    Count array size is max, but indices go from 0 to max, so size must be max + 1.
  2. Step 2: Identify impact of wrong size

    Without max + 1 size, count[max] index is undefined, causing errors or missing counts.
  3. Final Answer:

    Array count size should be max + 1 -> Option B
  4. Quick Check:

    Count array size = max + 1 [OK]
Hint: Count array size must cover max index + 1 [OK]
Common Mistakes:
  • Using max instead of max + 1 for array size
  • Confusing max with min
  • Ignoring array initialization
5. You have an array of integers ranging from 0 to 1000. Which sorting algorithm is best to sort this array efficiently and why?
hard
A. Quick Sort, because it has average O(n log n) time
B. Merge Sort, because it is stable and uses divide and conquer
C. Counting Sort, because it sorts in O(n + k) where k is range size
D. Heap Sort, because it uses a binary heap structure

Solution

  1. Step 1: Analyze input constraints

    Array elements range from 0 to 1000, so range size k = 1001, which is small and fixed.
  2. Step 2: Choose sorting algorithm based on range

    Counting Sort runs in O(n + k), which is efficient for small k, better than O(n log n) comparison sorts.
  3. Step 3: Confirm Counting Sort suitability

    Counting Sort is non-comparison based and ideal for small integer ranges.
  4. Final Answer:

    Counting Sort, because it sorts in O(n + k) where k is range size -> Option C
  5. Quick Check:

    Small range + Counting Sort = efficient sorting [OK]
Hint: Use Counting Sort for small integer ranges [OK]
Common Mistakes:
  • Choosing Quick Sort ignoring range size
  • Assuming Merge Sort is always best
  • Not considering non-comparison sorts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard