📖 Scenario: You work in a small shop that needs to organize its product prices quickly. Sometimes you sort prices by comparing them, and sometimes you use special tricks that don't compare each price directly.
🎯 Goal: You will create two lists of product prices. Then you will set a limit price. Next, you will find all prices less than the limit using a comparison method. Finally, you will print those prices.
📋 What You'll Learn
Create a list of integers called prices with these exact values: 50, 20, 70, 10, 40
Create an integer variable called limitPrice and set it to 40
Use a List<int> called filteredPrices to store prices less than limitPrice using a foreach loop and comparison
Print the filteredPrices list elements separated by spaces
💡 Why This Matters
🌍 Real World
Filtering product prices quickly helps shops show affordable options to customers.
💼 Career
Understanding comparison-based filtering and sorting is key for software developers working with data.
Progress0 / 4 steps
1
Create the list of product prices
Create a list of integers called prices with these exact values: 50, 20, 70, 10, 40
DSA C#
Hint
Use List<int> and initialize it with the exact values inside curly braces.
2
Set the limit price
Create an integer variable called limitPrice and set it to 40
DSA C#
Hint
Use int limitPrice = 40; to create the variable.
3
Filter prices less than the limit using comparison
Create a list called filteredPrices and use a foreach loop with variables price to add prices less than limitPrice to filteredPrices
DSA C#
Hint
Use List<int> filteredPrices = new List<int>(); and a foreach loop with if to add prices less than limitPrice.
4
Print the filtered prices
Use a foreach loop with variable price to print each element of filteredPrices separated by spaces
DSA C#
Hint
Use a foreach loop to print each price followed by a space.
Practice
(1/5)
1. Which of the following sorting algorithms is non-comparison based?
easy
A. Merge Sort
B. Quick Sort
C. Counting 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 frequencies and does not compare elements directly.
Final Answer:
Counting Sort -> Option C
Quick Check:
Non-comparison based = Counting Sort [OK]
Hint: Non-comparison sorts use counting or buckets, not element comparisons [OK]
Common Mistakes:
Confusing Quick Sort as non-comparison
Thinking Heap Sort is non-comparison
Assuming all sorts compare elements
2. Which of the following is the correct syntax to call Radix Sort method named RadixSort on an integer array arr in C#?
easy
A. RadixSort(arr);
B. arr.RadixSort();
C. RadixSort<int>(arr);
D. RadixSort arr;
Solution
Step 1: Understand method call syntax in C#
Calling a static method with an array argument uses method name and parentheses with the array inside.
Step 2: Analyze options
RadixSort(arr); uses correct syntax: RadixSort(arr); arr.RadixSort(); tries to call as instance method which is incorrect unless defined so. RadixSort<int>(arr); uses generic syntax unnecessarily. RadixSort arr; is invalid syntax.
Final Answer:
RadixSort(arr); -> Option A
Quick Check:
Static method call = RadixSort(arr); [OK]
Hint: Static method calls use MethodName(args) syntax [OK]
Common Mistakes:
Using instance method call on array
Adding unnecessary generic syntax
Writing invalid declaration instead of call
3. What will be the output of the following C# code snippet?
int[] arr = {4, 2, 2, 8, 3, 3, 1};
CountingSort(arr);
Console.WriteLine(string.Join(", ", arr));
void CountingSort(int[] array) {
int max = array.Max();
int[] count = new int[max + 1];
foreach (int num in array) count[num]++;
int index = 0;
for (int i = 0; i <= max; i++) {
while (count[i] > 0) {
array[index++] = i;
count[i]--;
}
}
}
medium
A. 8, 4, 3, 3, 2, 2, 1
B. 1, 2, 2, 3, 3, 4, 8
C. 4, 2, 2, 8, 3, 3, 1
D. 1, 1, 2, 2, 3, 3, 4
Solution
Step 1: Understand Counting Sort logic
Counting Sort counts occurrences of each number and then writes them back in sorted order.