Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Documenting this method Please help me explain this code line by line by helping me document this method above the // TODO: document

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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

=+How might these stem from country and regional cultures?

Answered: 1 week ago