0
0
DSA Javascriptprogramming~30 mins

Comparison Based vs Non Comparison Based Sorting in DSA Javascript - Build Both Approaches

Choose your learning style9 modes available
Comparison Based vs Non Comparison Based Sorting
📖 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
💡 Why This Matters
🌍 Real World
Sorting prices helps customers find cheaper products quickly in online stores.
💼 Career
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.