Java: Documenting this method Please help me explain this code line by line by h
ID: 3847462 • Letter: J
Question
Java: Documenting this method
Please help me explain this code line by line by helping me document this method above the "// TODO: document this method" in the code below for countingSort and radixSort. I already implemented the method, but just need help documenting the method for countingSort and radixSort line by line.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class PA2 {
// TODO: document this method
public static int[] countingSort(int[] a) {
//TODO: implement this method
if (a.length == 0) {
return a;
}
int max = a[0];
for (int i = 1; i < a.length; i++)
if (a[i] > max)
max = a[i];
int[] count = new int[max > a.length ? max + 1 : a.length + 1];
int[] sorted = new int[a.length];
for (int element = 0; element < a.length; element++)
count[a[element]]++;
for (int i = 0, j = 0; i < count.length; i++)
{
while (count[i] != 0)
{
sorted[j] = i;
count[i]--;
j++;
}
}
return sorted;
}
// TODO: document this method
public static int[] radixSort(int[] a) {
// TODO: implement this method
if (a.length == 0) {
return a;
}
int max = a[0];
int num = 1;
int[] arr2 = new int[1000]; // Array2 size of 1000
for (int i = 1; i < a.length; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
// System.out.println("hello");
// int d = String.valueOf(max).length();
while (max / num > 0) {
int[] arr = new int[10];
for (int i = 0; i < a.length; i++)
arr[((a[i] / num) % 10)]++;
for (int i = 1; i < arr.length; i++)
arr[i] += arr[i - 1];
for (int i = a.length - 1; i >= 0; i--)
arr2[--arr[(a[i] / num) % 10]] = a[i];
for (int i = 0; i < a.length; i++)
a[i] = arr2[i];
num *= 10;
}
return a;
}
Explanation / Answer
public class PA2 {
// TODO: document this method
public static int[] countingSort(int[] a) {
// If the array is empty, just return
if (a.length == 0) {
return a;
}
// assume first element to be the max, we will later modify it accordingly
int max = a[0];
// find the maximum element in array
for (int i = 1; i < a.length; i++)
if (a[i] > max)
max = a[i];
// Create array to store the counts
// This should be sufficient to hold elements equal to max element
int[] count = new int[max > a.length ? max + 1 : a.length + 1];
// to store the result in sorted order
int[] sorted = new int[a.length];
// Count how many times a elmenet has appeared in array
for (int element = 0; element < a.length; element++)
count[a[element]]++;
// Now populate the sorted aray based on count
// So if count array is 0,2,1,2 for lets say 4 elements from 0 to 3
// then result would be 1,1,2,3,3
for (int i = 0, j = 0; i < count.length; i++) {
while (count[i] != 0) {
sorted[j] = i;
count[i]--;
j++;
}
}
// return result
return sorted;
}
// TODO: document this method
public static int[] radixSort(int[] a) {
// If the array is empty, just return
if (a.length == 0) {
return a;
}
// assume first element to be the max, we will later modify it accordingly
int max = a[0];
// find the maximum element in array
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
int num = 1;
int[] arr2 = new int[1000]; // Array2 size of 1000
// In each iteration, num will be multiplied by 10
// and we try to sort the current digit
while (max / num > 0) {
// temporary array
int[] arr = new int[10];
// Store the count for the positions of current digit in temp array
for (int i = 0; i < a.length; i++)
arr[((a[i] / num) % 10)]++;
// Change arr[i] so that arr[i] now contains actual
// position of this digit in output
for (int i = 1; i < arr.length; i++)
arr[i] += arr[i - 1];
// build output array using the count above
for (int i = a.length - 1; i >= 0; i--)
arr2[--arr[(a[i] / num) % 10]] = a[i];
// Copy to output array, so that it now
// contains sorted numbers according to current digit
for (int i = 0; i < a.length; i++)
a[i] = arr2[i];
// increase the base to shift towards right digit
num *= 10;
}
return a;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.