Complete the code to compare two numbers for sorting in ascending order.
function compare(a, b) {
return a [1] b;
}The comparison function should return a negative value if a is less than b, so a - b is correct.
Complete the code to check if a sorting algorithm is comparison based by comparing elements.
if (arr[i] [1] arr[j]) { // swap elements }
Comparison based sorting algorithms compare elements using operators like > to decide order.
Fix the error in the non-comparison based counting sort code to update the count array correctly.
for (let i = 0; i < arr.length; i++) { count[arr[i]] [1] 1; }
Counting sort increments the count of each element, so count[arr[i]] += 1 is correct.
Fill both blanks to complete the radix sort digit extraction and counting step.
let digit = Math.floor((num [1] place) [2] 10);
To get the digit at a certain place, divide by place and then take modulo 10.
Fill all three blanks to complete the bucket sort insertion and sorting steps.
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);
Bucket index is found by flooring the number. Sorting uses subtraction for comparison.