Based on the code bellow, could someone please make a Javadoc that will describe
ID: 3863157 • Letter: B
Question
Based on the code bellow, could someone please make a Javadoc that will describe each field and method of a class, its diffrent from pseudo code, it should be a list that code be written into a word document, not comments in the code.
bellow is the code, Thank you!
import java.util.Random;
public class TestClass {
public static void main(String[] args) {
int bubbleValues[] = new int[20];
int selectionValues[] = new int[20];
int insertionValues[] = new int[20];
int quickValues[] = new int[20];
Random input = new Random();
for(int i = 0; i < 20; i++){
bubbleValues[i] = input.nextInt(100);
selectionValues[i] = input.nextInt(100);
insertionValues[i] = input.nextInt(100);
quickValues[i] = input.nextInt(100);
}
SortingBenchMarks in = new SortingBenchMarks();
int bubbleCount = in.bubbleSort(bubbleValues);
int selectionCount = in.selectionSort(selectionValues);
int insertionCount = in.insertionSort(insertionValues);
int quickCount = in.quickSort(quickValues);
System.out.println("Elements in the bubble array: ");
for(int i =0; i < 20; i++){
System.out.print(" " +bubbleValues[i]);
}
System.out.println();
System.out.println("Elements in the selection array:");
for(int i = 0; i <20; i++){
System.out.print( " " +selectionValues[i]);
}
System.out.println();
System.out.println("Elements in the insertion array:");
for(int i = 0; i <20; i++){
System.out.print( " " +insertionValues[i]);
}
System.out.println();
System.out.println("Elements in the quick array:");
for(int i = 0; i <20; i++){
System.out.print( " " +quickValues[i]);
}
System.out.println();
System.out.println("Bubble swaps: " +bubbleCount);
System.out.println("Selection swaps: " +selectionCount);
System.out.println("Insertion swaps: " +insertionCount);
System.out.println("Quick swaps: " +quickCount);
}
}
=============================================================================================
public class SortingBenchMarks {
int bubbleCount = 0;
int selectionCount = 0;
int insertionCount = 0;
int quickCount = 0;
public int bubbleSort(int[] array)
{
int maxElement;
int index;
int temp;
for(maxElement = array.length - 1; maxElement >=0; maxElement--){
for(index = 0; index <= maxElement - 1; index++){
if(array[index] > array[index + 1]){
temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
bubbleCount += 2;
}
}
}
return bubbleCount;
}
public int selectionSort(int[] array) {
int strtScan;
int indx;
int minIndx;
int miniValue;
for(strtScan = 0; strtScan < (array.length-1); strtScan++){
minIndx = strtScan;
miniValue = array[strtScan];
for(indx = strtScan + 1; indx < array.length; indx++){
if(array[indx] < miniValue){
miniValue = array[indx];
minIndx = indx;
}
}
array[minIndx] = array[strtScan];
array[strtScan] = miniValue;
selectionCount += 2;
}
return selectionCount;
}
public int insertionSort(int[] array){
int unsortedValue;
int scan;
for(int index = 1; index < array.length; index++){
unsortedValue = array[index];
scan = index;
while(scan > 0 && array[scan - 1] > unsortedValue){
array[scan] = array[scan - 1];
scan--;
insertionCount++;
}
array[scan] = unsortedValue;
insertionCount++;
}
return insertionCount;
}
public int quickSort(int array[]){
doQuickSort(array, 0, array.length - 1);
return quickCount;
}
private void doQuickSort(int array[], int start, int end){
int pivotPoint;
if(start < end){
pivotPoint = partition(array, start, end);
doQuickSort(array, start, pivotPoint - 1);
doQuickSort(array, pivotPoint + 1, end);
}
}
private int partition(int array[], int start, int end){
int pivotValue;
int endOfLeftList;
int mid;
mid = (start + end) / 2;
swap(array, start, mid);
pivotValue = array[start];
endOfLeftList = start;
for(int scan = start + 1; scan <= end; scan++){
if(array[scan] < pivotValue){
endOfLeftList++;
swap(array, endOfLeftList, scan);
}
}
swap(array, start, endOfLeftList);
return endOfLeftList;
}
private void swap(int[] array, int a, int b){
int temp;
temp = array[a];
array[a] = array[b];
array[b] = temp;
quickCount += 2;
}
}
Explanation / Answer
The above program gives the number of swaps done in different sorting algorithms i.e. Bubble Sort, Selection Sort, Insertion Sort and Quick Sort.
1. Description of clas SortingBenchMarks:
The member variables aim to store the no. of swaps performed in each kind of sorting algorithm.
No. of swaps done in bubble sort gets stored in member variable bubbleCount which is of integer data type and has default value 0 i.e,
int bubbleCount = 0;
No. of swaps done in selection sort gets stored in member variable selectionCount which is of integer data type and has default value 0 i.e,
int selectionCount = 0;
No. of swaps done in insertion sort gets stored in member variable insertionCount which is of integer data type and has default value 0 i.e,
int insertionCount = 0;
No. of swaps done in quick sort gets stored in member variable quickCount which is of integer data type and has default value 0 i.e,
int quickCount = 0;
The member functions are the implementation of different sorting algorithms which return the total no. of swaps done in the array to sort it.
To perform selection sort on the array,
public int selectionSort(int [] array) is called. It returns the total no. of swaps done in the array to swap it and updates seletionCount to no. of swaps.
To perform bubble sort on the array,
public int bubbleSort(int [] array) is called. It returns the total no. of swaps done in the array to swap it and updates bubbleCount to no. of swaps.
To perform insertion sort on the array,
public int insertionSort(int [] array) is called. It returns the total no. of swaps done in the array to swap it and updates insertionCount to no. of swaps.
To perform quick sort on the array,
public int quickSort(int [] array) is called. It returns the total no. of swaps done in the array to swap it and updates quickCount to no. of swaps.
doQuickSort() and Partition() methods can be called from QuickSort only and thusthey are declared private so that they can not be acccesed from outside the class.
partition() is used to find the pivot element of quick sort and doQuickSort() is used to perform quick sort on partitioned array.
In the main() class:
4 Different arrays of size 20 are created for performing sorting using different sorting algorithms on each array.
for bubble sort:int bubbleValues[] = new int[20];
for selection sort:
int selectionValues[] = new int[20];
for insertion sort:
int insertionValues[] = new int[20];
for quick sort:
int quickValues[] = new int[20];
all the 4 arrays are filled with random integers from 0 to 100 using:
Random input = new Random();
for(int i = 0; i < 20; i++){
bubbleValues[i] = input.nextInt(100);
selectionValues[i] = input.nextInt(100);
insertionValues[i] = input.nextInt(100);
quickValues[i] = input.nextInt(100);
}
in object of SortingBenchClass is created and all the 4 member variables are updated using:
SortingBenchMarks in = new SortingBenchMarks();
int bubbleCount = in.bubbleSort(bubbleValues);
int selectionCount = in.selectionSort(selectionValues);
int insertionCount = in.insertionSort(insertionValues);
int quickCount = in.quickSort(quickValues);
After performing the sort, all the values of different arrays are displayed using:
System.out.println("Elements in the bubble array: ");
for(int i =0; i < 20; i++){
System.out.print(" " +bubbleValues[i]);
}
System.out.println();
System.out.println("Elements in the selection array:");
for(int i = 0; i <20; i++){
System.out.print( " " +selectionValues[i]);
}
System.out.println();
System.out.println("Elements in the insertion array:");
for(int i = 0; i <20; i++){
System.out.print( " " +insertionValues[i]);
}
System.out.println();
System.out.println("Elements in the quick array:");
for(int i = 0; i <20; i++){
System.out.print( " " +quickValues[i]);
}
System.out.println();
and then the no. of swaps in each sorting algorithm are displayed using:
System.out.println("Bubble swaps: " +bubbleCount);
System.out.println("Selection swaps: " +selectionCount);
System.out.println("Insertion swaps: " +insertionCount);
System.out.println("Quick swaps: " +quickCount);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.