Sorting methods that decide the order of elements by comparing them with each other, like checking if one number is bigger or smaller than another.
Click to reveal answer
beginner
Name two common comparison based sorting algorithms.
Bubble Sort and Quick Sort are examples of comparison based sorting algorithms.
Click to reveal answer
beginner
What is non comparison based sorting?
Sorting methods that do not compare elements directly but use other information like counting or grouping to sort, such as counting sort or radix sort.
Click to reveal answer
intermediate
Why can non comparison based sorting be faster than comparison based sorting?
Because they use special tricks like counting or grouping, they can sort in linear time for certain types of data, unlike comparison based sorting which usually takes longer.
Click to reveal answer
intermediate
What is the best time complexity possible for comparison based sorting?
The best possible time complexity for comparison based sorting is O(n log n), meaning it takes at least that much time to sort n items by comparing.
Click to reveal answer
Which of these is a comparison based sorting algorithm?
AQuick Sort
BCounting Sort
CRadix Sort
DBucket Sort
✗ Incorrect
Quick Sort sorts by comparing elements, so it is comparison based.
Non comparison based sorting algorithms usually work best when:
AData is already sorted
BData is very large and random
CData has a limited range of values
DData is strings only
✗ Incorrect
Non comparison based sorting like counting sort works best when data values are within a limited range.
What is the lower bound time complexity for comparison based sorting?
AO(n log n)
BO(n^2)
CO(n)
DO(log n)
✗ Incorrect
Comparison based sorting cannot be faster than O(n log n) in the general case.
Which sorting method uses counting to sort elements?
AMerge Sort
BHeap Sort
CQuick Sort
DCounting Sort
✗ Incorrect
Counting Sort counts the number of occurrences of each value to sort.
Radix Sort is an example of:
AComparison based sorting
BNon comparison based sorting
CDivide and conquer sorting
DIn-place sorting
✗ Incorrect
Radix Sort sorts numbers digit by digit without comparing elements directly.
Explain the main difference between comparison based and non comparison based sorting.
Think about how the algorithms decide the order of elements.
You got /3 concepts.
Describe a scenario where non comparison based sorting is more efficient than comparison based sorting.
Consider when counting or grouping helps speed up sorting.
You got /3 concepts.
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
Step 1: Understand comparison based sorting
Comparison based sorting algorithms decide order by comparing elements, like Merge Sort, Quick Sort, and Heap Sort.
Step 2: Identify non-comparison based sorting
Counting Sort uses counting of element frequencies instead of comparisons, so it is non-comparison based.
Final Answer:
Counting Sort -> Option D
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
Step 1: Recall JavaScript function call syntax
Functions are called with parentheses enclosing arguments, like func(args).
Step 2: Match syntax with options
Only countingSort(arr); uses correct parentheses syntax for function call.
Final Answer:
countingSort(arr); -> Option A
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));