Complete the code to perform a comparison-based sort using Arrays.sort.
int[] arr = {5, 3, 8, 1};
java.util.Arrays.[1](arr);
System.out.println(java.util.Arrays.toString(arr));The Arrays.sort method sorts the array using comparison-based sorting.
Complete the code to count the frequency of digits using a non-comparison based approach.
int[] digits = {1, 3, 2, 1, 3, 1};
int[] freq = new int[10];
for (int d : digits) {
freq[[1]]++;
}
System.out.println(java.util.Arrays.toString(freq));We use the digit value d as the index to increment frequency count.
Fix the error in the counting sort implementation by filling the correct index for frequency increment.
int[] arr = {4, 2, 2, 8, 3, 3, 1};
int max = 8;
int[] count = new int[max + 1];
for (int num : arr) {
count[[1]]++;
}
System.out.println(java.util.Arrays.toString(count));The variable num holds the current element and is used as index to increment count.
Fill both blanks to complete the radix sort digit extraction and counting step.
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
int exp = 1;
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
for (int i = 0; i < n; i++) {
count[(arr[i] / [1]) % [2]]++;
}In radix sort, digits are extracted by dividing by exp and taking modulo 10.
Fill all three blanks to complete the bucket sort insertion step.
int[] arr = {0, 42, 32, 33, 52, 37, 47, 51};
int n = arr.length;
java.util.List<java.util.List<Integer>> buckets = new java.util.ArrayList<>();
for (int i = 0; i < 10; i++) {
buckets.add(new java.util.ArrayList<>());
}
for (int num : arr) {
int index = num / [1];
buckets.get(index).[2](num);
}
for (int i = 0; i < 10; i++) {
java.util.Collections.[3](buckets.get(i));
}Bucket index is calculated by dividing by 10, numbers are added to buckets, and each bucket is sorted.
