Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

commenting what each line of code does Write a Java sorting application with, tw

ID: 669976 • Letter: C

Question

commenting what each line of code does Write a Java sorting application with, two classes javaSort and javasorttest. The javasort class must contain sorting methods for bubblesort, insertion sort, shellsort ,

Mergesort and quicksort. The application has to generate : an array of 13 random intergers from 1-99, -then prompt the user to select a sorting option( bubblesort, insertion sort, shellsort ,

Mergesort or quicksort) the application must account for duplications and Show each completed pass of the sort on a new line with arrays surrounded by square brackets until sorted

Explanation / Answer

package mani;

import java.util.Random;
import java.util.Scanner;


public class javasorttest
{
  
     
public static void main(String[] args){
   int[] a=new int[13];
   Random r=new Random();
   for(int i=0;i<13;i++){
       a[i]=r.nextInt(99)+1;
   }
   for (int c = 0; c < 13; c++)
System.out.print(a[c]+",");
   System.out.println("");
   javasort j=new javasort();
   System.out.println("Select the sorting algo. 1.bubbleSort 2.insertionSort 3.shellSort 4.MergeSort 5.QuickSort.");
   Scanner s=new Scanner(System.in);
   int opt=s.nextInt();
   switch(opt){
   case 1:
       j.bubbleSort(a);
       break;
   case 2:
       j.insertionSort(a);
      
       break;
   case 3:
       j.shellSort(a);
       break;
   case 4:
       j.MergeSort(a, 0, 13);
       j.fPrint();
       break;
   case 5:
       j.quickSort(a ,0, 12);
       j.fPrint();
       break;
   }
}
}

package mani;

public class javasort
{
private int[] arr=new int[13];
public void bubbleSort(int[] a){
   int c,d,temp;
   for (c = 0; c < ( 13 - 1 ); c++) {
   for (d = 0; d < 13- c - 1; d++) {
   if (a[d] > a[d+1]) /* For descending order use < */
   {
   temp = a[d];
   a[d] = a[d+1];
   a[d+1] = temp;
   }
   }
   }
     
   System.out.print("Sorted list of numbers");
     
   for (c = 0; c < 13; c++)
   System.out.println(a[c]+",");
}
public void insertionSort(int[] a){
   int temp;
for (int i = 1; i < 13; i++) {
for(int j = i ; j > 0 ; j--){
if(a[j] < a[j-1]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
for (int c = 0; c < 13; c++)
   System.out.print(a[c]+",");

}
public void shellSort(int[] a){
   int increment = a.length / 2;
while (increment > 0)
{
for (int i = increment; i < a.length; i++)
{
int j = i;
int temp = a[i];
while (j >= increment && a[j - increment] > temp)
{
a[j] = a[j - increment];
j = j - increment;
}
a[j] = temp;
}
if (increment == 2)
increment = 1;
else
increment *= (5.0 / 11);

}
for (int c = 0; c < 13; c++)
   System.out.print(a[c]+",");
}
public void MergeSort(int[] a, int low, int high){
   int N = high - low;   
if (N <= 1)
return;
int mid = low + N/2;
// recursively sort
MergeSort(a, low, mid);
MergeSort(a, mid, high);
// merge two sorted subarrays
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j]<a[i])
temp[k] = a[j++];
else
temp[k] = a[i++];
}
for (int k = 0; k < N; k++)
a[low + k] = temp[k];
printM(a);

}
public void quickSort(int[] a,int low,int high){
   int i =low, j = high;
int temp;
int pivot = a[(low + high) / 2];

/** partition **/
while (i <= j)
{
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i <= j)
{
/** swap **/
temp = a[i];
a[i] = a[j];
a[j] = temp;

i++;
j--;
}
}
/** recursively sort lower half **/
if (low < j)
quickSort(a, low, j);
/** recursively sort upper half **/
if (i < high)
quickSort(a, i, high);
printM(a);

}
public void printM(int[] a){
   arr=a;
}
public void fPrint(){
   for (int c = 0; c < 13; c++)
   System.out.print(arr[c]+",");
}
}