Question
My program doesn't show each of the correct list it's being sorted, number of comparison and number of assignment except bubble sorting method. I think
My program doesn't show each of the correct list it's being sorted, number of comparison and number of assignment except bubble sorting method. I think I missed to reset the input string but could not figure out the way to reset input. Here is my code: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner;
/** * Create a program that does head-to-head comparisons between four sorting * methods: improved bubble sort; selection sort; insertion sort; and Shell * sort. * */ public class SortAlgorithms {
private static char tracing; private static char list;
private static int numAsgn = 0; private static int numComp = 0;
private static int size; private static int min; private static int max; private static final Scanner KBD = new Scanner(System.in);
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("How many elements do you want in the list? "); size = KBD.nextInt(); System.out.print("What are the smallest and largest values for lsit elements? "); min = KBD.nextInt(); max = KBD.nextInt();
KBD.nextLine(); pause();
System.out.print("Would you like to see the list before it's sorted? "); list = Character.toUpperCase(br.readLine().charAt(0));
System.out.print("Would you like to see the list as it's being sorted? "); tracing = Character.toUpperCase(br.readLine().charAt(0)); pause();
sortNumbers(); }
// prompt the user and wait for them to press enter private static void pause() { System.out.print(" ...Press enter..."); KBD.nextLine(); System.out.println(); }
/** * Sort a list of integer values, generated randomly. */ public static void sortNumbers() { resetCounts();
Integer[] numbers = randomNumbers(size, min, max); if (list == 'Y') { System.out.println("Here is the list: " + Arrays.toString(numbers)); pause(); }
System.out.printf(" %1s%25s%20s%20s ", "Method", "#COMP", "#ASGN", "#OPS");
bubbleSort(numbers); System.out.printf("%1s%25d%20d%20d ", "Bubble", numComp, numAsgn, numAsgn + numComp);
selectionSort(numbers); System.out.printf("%1s%22d%20d%20d ", "Selection", numComp, numAsgn, numAsgn + numComp);
insertionSort(numbers); System.out.printf("%1s%22d%20d%20d ", "Insertion", numComp, numAsgn, numAsgn + numComp);
shellSort(numbers); System.out.printf("%1s%26d%20d%20d ", "Shell", numComp, numAsgn, numAsgn + numComp); }
/** * Reset the operation counts to zero. */ public static void resetCounts() { numAsgn = 0; numComp = 0; }
/** * Generate an array of random values. * * @param howMany the length of the list to be generated. * @param lo the smallest value allowed in the list * @param hi the largest value allowed in the list */ public static Integer[] randomNumbers(int size, int min, int max) { int range = max - min + 1; Integer[] result = new Integer[size]; for (int i = 0; i < size; ++i) { result[i] = (int) (min + range * Math.random()); } return result; }
/** * Perform bubble sort on the given array. * * @param a the array to sort. */ public static
} }
/** * Perform insertion sort on the given array. * * @param a the array to sort. */ public static
}
/** * Perform selection sort on the given array. if tracing, show the array * after each selection round. * * @param a the array to sort */ public static
/** * Perform shell sort on the given array. * * @param a the array to sort. */ public static
if (gap % 2 == 0) { ++gap; } ++numComp; for (int i = gap; i < a.length; ++i) { ++numAsgn; int p = i; T temp = a[p]; ++numComp; while (p >= gap && a[p - gap].compareTo(temp) > 0) { a[p] = a[p - gap]; p -= gap; ++numAsgn; } a[p] = temp; ++numAsgn; } //if (tracing == 'Y') { System.out.println("...gap=" + gap + ": " + Arrays.toString(a)); // } gap /= 2;
} }
/** * Calculate how many operations a list of the given length should take. * * @param numItems the number of elements in a list. * @return the number of operations expected (on average) to sort that list */ private static int expect(int numItems) { return (numItems * numItems + numItems) * 5 / 4; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started