Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the maximum value in the array for Radix Sort.
DSA C++
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] [1] max) {
max = arr[i];
}
}
return max;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes the max to never update correctly.
✗ Incorrect
We compare each element with the current max and update max if the element is greater.
2fill in blank
mediumComplete the code to count the occurrences of digits for the current place value in Counting Sort used by Radix Sort.
DSA C++
void countSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++) {
count[(arr[i] / [1]) % 10]++;
}
// rest of the code...
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 10 instead of exp will always count the units digit only.
✗ Incorrect
We divide by the current exponent (place value) to isolate the digit we want to count.
3fill in blank
hardFix the error in the loop that builds the output array in Counting Sort for Radix Sort.
DSA C++
for (int i = n - 1; i >= 0; i--) { output[count[(arr[i] / exp) % 10] [1]] = arr[i]; count[(arr[i] / exp) % 10]--; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' will cause index overflow and incorrect placement.
✗ Incorrect
We decrement the count after placing the element to update the position for the next occurrence.
4fill in blank
hardFill both blanks to copy the output array back to the original array after Counting Sort step.
DSA C++
for (int i = 0; i [1] n; i++) { arr[i] = [2][i]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bounds error.
Copying from count instead of output causes wrong data.
✗ Incorrect
We loop from 0 to less than n and copy each element from output to arr.
5fill in blank
hardFill all three blanks to complete the main Radix Sort loop that calls Counting Sort for each digit place.
DSA C++
void radixSort(int arr[], int n) {
int m = getMax(arr, n);
for (int exp = 1; exp [1] m; exp = exp [2] 10) {
countSort(arr, n, [3]);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' causes the loop to never run.
Passing m instead of exp to countSort causes wrong sorting.
✗ Incorrect
We loop while exp is less than or equal to max, multiply exp by 10 each time, and pass exp to countSort.