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 properties like counting or grouping to sort, such as counting how many times a number appears.
Click to reveal answer
beginner
Give an example of a non comparison based sorting algorithm.
Counting Sort is an example where sorting is done by counting the number of occurrences of each value.
Click to reveal answer
intermediate
Which type of sorting can be faster than comparison based sorting for certain data, and why?
Non comparison based sorting can be faster when sorting numbers within a limited range because it uses counting or grouping instead of comparing each pair.
Click to reveal answer
Which of these is a comparison based sorting algorithm?
AMerge Sort
BCounting Sort
CRadix Sort
DBucket Sort
✗ Incorrect
Merge Sort compares elements to decide their order, so it is comparison based.
Non comparison based sorting algorithms usually work best when:
AData has a large range of values
BData is already sorted
CData values are within a small range
DData contains strings only
✗ Incorrect
Non comparison based sorting like Counting Sort is efficient when data values are within a small range.
What is the main operation in comparison based sorting?
AComparing two elements
BCounting occurrences
CGrouping elements by value
DUsing hash tables
✗ Incorrect
Comparison based sorting relies on comparing two elements to decide their order.
Which sorting algorithm is NOT comparison based?
AHeap Sort
BQuick Sort
CInsertion Sort
DCounting Sort
✗ Incorrect
Counting Sort does not compare elements; it counts occurrences instead.
What is a limitation of non comparison based sorting?
ACannot sort numbers
BOnly works on data with limited range
CAlways slower than comparison based sorting
DRequires recursive calls
✗ Incorrect
Non comparison based sorting usually requires data values to be within a limited range to be efficient.
Explain the difference between comparison based and non comparison based sorting.
Think about how the sorting decides the order of elements.
You got /3 concepts.
When would you choose a non comparison based sorting algorithm over a comparison based one?
Consider the data characteristics and performance.
You got /3 concepts.
Practice
(1/5)
1. Which of the following sorting algorithms is not based on comparing elements?
easy
A. Quick Sort
B. Merge Sort
C. Counting Sort
D. Heap Sort
Solution
Step 1: Understand comparison based sorting
Comparison based sorting algorithms decide order by comparing elements pairwise, like Merge Sort, Quick Sort, and Heap Sort.
Step 2: Identify non-comparison based sorting
Counting Sort uses counting frequencies and does not compare elements directly.
Final Answer:
Counting Sort -> Option C
Quick Check:
Non-comparison based = Counting Sort [OK]
Hint: Counting Sort uses counts, no element comparisons [OK]
Common Mistakes:
Confusing Counting Sort with Quick Sort
Thinking Merge Sort is non-comparison based
Assuming Heap Sort doesn't compare elements
2. Which of the following is the correct syntax to call a non-comparison based sorting function counting_sort(arr) in Python?
easy
A. counting_sort(arr)
B. counting_sort arr
C. counting_sort{arr}
D. counting_sort[arr]
Solution
Step 1: Recall Python function call syntax
Functions are called with parentheses enclosing arguments, like func(args).
Step 2: Match syntax with options
Only counting_sort(arr) uses correct parentheses syntax.
Final Answer:
counting_sort(arr) -> Option A
Quick Check:
Function call uses parentheses [OK]
Hint: Use parentheses () to call functions in Python [OK]
Common Mistakes:
Using square brackets instead of parentheses
Omitting parentheses when calling functions
Using curly braces which are invalid for calls
3. What will be the output of the following Python code using Counting Sort on the array [4, 2, 2, 8, 3, 3, 1]?
def counting_sort(arr):
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arr
print(counting_sort([4, 2, 2, 8, 3, 3, 1]))
medium
A. [1, 2, 2, 3, 3, 4, 8]
B. [8, 4, 3, 3, 2, 2, 1]
C. [1, 3, 2, 4, 8]
D. [4, 2, 2, 8, 3, 3, 1]
Solution
Step 1: Count frequencies of each number
Count array will have counts: 1->1, 2->2, 3->2, 4->1, 8->1, others 0.
Step 2: Build sorted array by repeating each number count times
Sorted array is [1, 2, 2, 3, 3, 4, 8].
Final Answer:
[1, 2, 2, 3, 3, 4, 8] -> Option A
Quick Check:
Counting Sort output = sorted array [OK]
Hint: Counting Sort outputs sorted array by frequency counts [OK]
Common Mistakes:
Confusing original array with sorted output
Reversing the sorted order
Missing duplicates in output
4. The following code attempts to implement Counting Sort but has a bug. What is the error?
def counting_sort(arr):
max_val = max(arr)
count = [0] * max_val
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arr
print(counting_sort([1, 4, 1, 2, 7, 5]))
medium
A. Using extend instead of append causes error
B. count array size is too small, missing last element count
C. max_val should be min_val
D. Loop range should be over arr, not count
Solution
Step 1: Check count array size
Count array size is max_val, but should be max_val + 1 to include max element index.
Step 2: Identify impact of size error
Missing last index causes IndexError or missing counts for max element.
Final Answer:
count array size is too small, missing last element count -> Option B
Quick Check:
Count array size = max_val + 1 needed [OK]
Hint: Count array must cover all values including max [OK]
Common Mistakes:
Ignoring off-by-one error in count array size
Thinking extend causes error
Confusing max_val with min_val
5. You have a large list of integers ranging from 0 to 1000. Which sorting algorithm is best to sort this list efficiently and why?
A) Quick Sort because it has average O(n log n) time
B) Counting Sort because it sorts in O(n + k) time where k is max value
C) Merge Sort because it is stable and always O(n log n)
D) Bubble Sort because it is simple to implement
hard
A. Quick Sort because it has average O(n log n) time
B. Bubble Sort because it is simple to implement
C. Merge Sort because it is stable and always O(n log n)
D. Counting Sort because it sorts in O(n + k) time where k is max value
Solution
Step 1: Analyze input range and size
Input values are integers from 0 to 1000, so k = 1000 is small compared to n (large list).
Step 2: Choose sorting algorithm based on time complexity
Counting Sort runs in O(n + k), which is efficient for small k and large n, better than O(n log n).
Final Answer:
Counting Sort because it sorts in O(n + k) time where k is max value -> Option D
Quick Check:
Small range + large list = Counting Sort best [OK]
Hint: Use Counting Sort for small range large list [OK]
Common Mistakes:
Choosing Quick Sort ignoring input range
Preferring Bubble Sort despite poor performance
Ignoring Counting Sort's linear time advantage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.