📖 Scenario: You work in a warehouse where you need to sort boxes by their weight quickly. Each box's weight is a small whole number. You want to organize them from lightest to heaviest using a fast method.
🎯 Goal: Build a program that sorts a list of box weights using the counting sort algorithm. This method counts how many boxes have each weight and then arranges them in order.
📋 What You'll Learn
Create a slice called weights with exact values: 4, 2, 2, 8, 3, 3, 1
Create a variable called maxWeight to store the highest weight in weights
Implement counting sort logic using a count slice to store frequencies
Print the sorted slice of weights
💡 Why This Matters
🌍 Real World
Counting sort is useful in warehouses or factories where items have small integer weights or categories and need fast sorting.
💼 Career
Understanding counting sort helps in roles like software development and data processing where efficient sorting of limited-range data is required.
Progress0 / 4 steps
1
Create the initial weights slice
Create a slice called weights with these exact values: 4, 2, 2, 8, 3, 3, 1.
DSA Go
Hint
Use weights := []int{4, 2, 2, 8, 3, 3, 1} to create the slice.
2
Find the maximum weight
Create a variable called maxWeight and set it to the highest value in the weights slice by checking each element.
DSA Go
Hint
Start with the first weight as maxWeight. Then check each weight and update maxWeight if you find a bigger one.
3
Implement counting sort logic
Create a slice called count with length maxWeight + 1 to store counts. Then, use a for loop with variables _, w to count how many times each weight appears in weights. Next, create a slice called sorted to store the sorted weights. Use a for loop with variable weight from 0 to maxWeight to append each weight to sorted as many times as it appears in count.
DSA Go
Hint
Use make([]int, maxWeight+1) to create the count slice. Count each weight, then build the sorted slice by repeating weights according to their counts.
4
Print the sorted weights
Use fmt.Println(sorted) to print the sorted slice of weights.
DSA Go
Hint
Use fmt.Println(sorted) to show the sorted weights.
Practice
(1/5)
1. What is the main idea behind the Counting Sort Algorithm?
easy
A. Swapping adjacent elements if they are in wrong order
B. Comparing each element with every other element
C. Counting the number of occurrences of each element to sort
D. Dividing the array into halves and sorting recursively
Solution
Step 1: Understand Counting Sort principle
Counting sort counts how many times each value appears in the input.
Step 2: Use counts to place elements in order
Using these counts, it places elements directly in sorted order without comparisons.
Final Answer:
Counting the number of occurrences of each element to sort -> Option C
Quick Check:
Counting occurrences = D [OK]
Hint: Counting sort counts elements, no comparisons needed [OK]
Common Mistakes:
Thinking it compares elements like bubble sort
Confusing with divide and conquer methods
Assuming it swaps elements repeatedly
2. Which of the following is the correct way to declare the count array in Go for counting sort when input values range from 0 to 9?
easy
A. count := make([]int, 10)
B. count := make([]int, 9)
C. count := make([]int, 11)
D. count := make([]int, 0)
Solution
Step 1: Determine size of count array
For values 0 to 9, we need 10 slots to count each number.
Step 2: Use make function with correct length
Using make([]int, 10) creates an integer slice of length 10, perfect for counting 0-9.
Final Answer:
count := make([]int, 10) -> Option A
Quick Check:
Length 10 for 0-9 range = A [OK]
Hint: Count array length = max value + 1 [OK]
Common Mistakes:
Using length 9 misses counting number 9
Using length 0 creates empty slice
Using length 11 wastes space unnecessarily
3. What will be the output of the following Go code snippet using counting sort logic?
arr := []int{4, 2, 2, 8, 3, 3, 1}
count := make([]int, 9)
for _, v := range arr {
count[v]++
}
var sorted []int
for i, c := range count {
for j := 0; j < c; j++ {
sorted = append(sorted, i)
}
}
fmt.Println(sorted)