📖 Scenario: You are working in a small online store. You want to sort the list of product prices to show the cheapest items first. You will learn two ways to sort: one by comparing prices directly, and another by using counting since prices are whole numbers.
🎯 Goal: Build two sorting methods: one using comparison (like bubble sort) and one using counting sort (non-comparison). Then print the sorted prices from both methods.
📋 What You'll Learn
Create an array called prices with exact values: [40, 10, 30, 20, 10]
Create a variable called maxPrice and set it to 40
Write a function called bubbleSort that sorts the prices array using comparison
Write a function called countingSort that sorts the prices array using counting sort and maxPrice
Print the sorted arrays from both bubbleSort and countingSort
Understanding different sorting methods is important for optimizing software performance in real-world applications.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with these exact values: [40, 10, 30, 20, 10]
DSA Javascript
Hint
Use const prices = [40, 10, 30, 20, 10]; to create the array.
2
Set the maximum price
Create a variable called maxPrice and set it to 40
DSA Javascript
Hint
Use const maxPrice = 40; to store the highest price.
3
Write bubbleSort and countingSort functions
Write a function called bubbleSort that sorts the prices array using comparison. Then write a function called countingSort that sorts the prices array using counting sort and maxPrice
DSA Javascript
Hint
Use nested loops for bubbleSort. Use a count array for countingSort.
4
Print sorted arrays
Print the sorted arrays from both bubbleSort(prices.slice()) and countingSort(prices, maxPrice)
DSA Javascript
Hint
Use console.log(bubbleSort(prices.slice())) and console.log(countingSort(prices, maxPrice)) to print sorted arrays.
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));