📖 Scenario: You are working with a list of numbers that need to be sorted. Sometimes, sorting is done by comparing numbers directly. Other times, sorting can be done without comparing numbers, using their properties like digits.
🎯 Goal: You will create a Go program that first sorts a list of numbers using a comparison-based method (Bubble Sort), then sorts the same list using a non-comparison-based method (Counting Sort). You will see how both methods work and their results.
📋 What You'll Learn
Create a slice of integers with exact values
Create a variable for the maximum value in the slice
Implement Bubble Sort to sort the slice using comparisons
Implement Counting Sort to sort the slice without direct comparisons
Print the sorted slices after each sorting method
💡 Why This Matters
🌍 Real World
Sorting is used everywhere: organizing data, searching faster, and making decisions. Knowing different sorting methods helps choose the best one for the task.
💼 Career
Software developers often need to sort data efficiently. Understanding comparison and non-comparison sorting helps in optimizing programs and solving problems faster.
Progress0 / 4 steps
1
Create the initial slice of integers
Create a slice called numbers with these exact integers: 4, 2, 7, 3, 8, 5, 1
DSA Go
Hint
Use numbers := []int{4, 2, 7, 3, 8, 5, 1} to create the slice.
2
Create a variable for the maximum value
Create a variable called maxVal and set it to 8, which is the maximum value in numbers
DSA Go
Hint
Use maxVal := 8 to store the maximum value.
3
Implement Bubble Sort (Comparison Based Sorting)
Write a nested for loop to perform Bubble Sort on numbers. Use variables i and j for loops. Swap elements if numbers[j] > numbers[j+1].
DSA Go
Hint
Use two loops with i and j. Swap adjacent elements if the left one is bigger.
4
Implement Counting Sort (Non Comparison Based Sorting) and print results
Create a slice called count of size maxVal + 1 initialized with zeros. Count occurrences of each number in numbers. Then overwrite numbers with sorted values using count. Finally, print numbers after Bubble Sort and after Counting Sort.
DSA Go
Hint
Use a count slice to store counts. Then rebuild numbers in order. Use fmt.Println to print results.
Practice
(1/5)
1. Which of the following sorting algorithms is non-comparison based?
easy
A. Merge Sort
B. Counting Sort
C. Quick Sort
D. Heap 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 and indexing, not direct comparisons, so it is non-comparison based.
Final Answer:
Counting Sort -> Option B
Quick Check:
Counting Sort = Non-comparison based [OK]
Hint: Counting Sort uses counts, not comparisons [OK]
Common Mistakes:
Confusing Counting Sort with Quick Sort
Thinking Merge Sort is non-comparison based
Assuming Heap Sort is non-comparison based
2. Which of the following is the correct syntax to call a non-comparison based sorting function CountingSort(arr []int) in Go?
easy
A. CountingSort{arr}
B. CountingSort arr
C. CountingSort(arr)
D. CountingSort->arr
Solution
Step 1: Recall Go function call syntax
In Go, functions are called with the function name followed by parentheses enclosing arguments.
Step 2: Apply syntax to CountingSort
Calling CountingSort with array arr is done as CountingSort(arr).
Final Answer:
CountingSort(arr) -> Option C
Quick Check:
Function call syntax = CountingSort(arr) [OK]
Hint: Use parentheses for function calls in Go [OK]
Common Mistakes:
Omitting parentheses in function call
Using braces {} instead of parentheses
Using arrow notation which is invalid in Go
3. What will be the output of the following Go code snippet?