0
0
DSA Javascriptprogramming~10 mins

Comparison Based vs Non Comparison Based Sorting in DSA Javascript - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compare two numbers for sorting in ascending order.

DSA Javascript
function compare(a, b) {
  return a [1] b;
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction
Using multiplication or division which don't give order
2fill in blank
medium

Complete the code to check if a sorting algorithm is comparison based by comparing elements.

DSA Javascript
if (arr[i] [1] arr[j]) {
  // swap elements
}
Drag options to blanks, or click blank then click option'
A!=
B=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality instead of comparison
Using less than or equal which changes logic
3fill in blank
hard

Fix the error in the non-comparison based counting sort code to update the count array correctly.

DSA Javascript
for (let i = 0; i < arr.length; i++) {
  count[arr[i]] [1] 1;
}
Drag options to blanks, or click blank then click option'
A=
B-=
C+=
D*=
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment instead of increment
Using subtraction or multiplication which are incorrect
4fill in blank
hard

Fill both blanks to complete the radix sort digit extraction and counting step.

DSA Javascript
let digit = Math.floor((num [1] place) [2] 10);
Drag options to blanks, or click blank then click option'
A%
B/
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping division and modulo
Using multiplication or addition which are incorrect
5fill in blank
hard

Fill all three blanks to complete the bucket sort insertion and sorting steps.

DSA Javascript
buckets[[1]].push(num);
for (let i = 0; i < buckets.length; i++) {
  buckets[i].sort((a, b) => a [2] b);
}
let sorted = [].concat(...buckets).sort((a, b) => a [3] b);
Drag options to blanks, or click blank then click option'
AMath.floor(num)
B-
C+
Dnum % 10
Attempts:
3 left
💡 Hint
Common Mistakes
Using modulo for bucket index
Using addition instead of subtraction in sort