Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 > void bubbleSort(T[] a) { for (int i = a.length - 1; i > 0; --i) { boolean elementSwapped = false; //numComp++; for (int j = 0; j < i; ++j) { numComp++; if (a[j].compareTo(a[j + 1]) > 0) { numAsgn += 3; T temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; elementSwapped = true; } } if (!elementSwapped) { break; } //if (tracing == 'Y') { System.out.println("one more bubbled up: " + Arrays.toString(a)); //}

} }

/** * Perform insertion sort on the given array. * * @param a the array to sort. */ public static > void insertionSort(T[] a) { for (int i = 0; i < a.length - 1; ++i) { int p = i + 1; T temp = a[p]; ++numAsgn; while (p > 0 && a[p - 1].compareTo(temp) > 0) { ++numComp; a[p] = a[p - 1]; ++numAsgn; --p; } if (p > 0) { ++numComp; // count the last a[p-1] comparison } a[p] = temp; ++numAsgn; //if (tracing == 'Y') { System.out.println("one more inserted: " + Arrays.toString(a)); //} }

}

/** * Perform selection sort on the given array. if tracing, show the array * after each selection round. * * @param a the array to sort */ public static > void selectionSort(T[] a) { for (int i = 0; i < a.length - 1; ++i) { int p = i; ++numAsgn; for (int j = i + 1; j < a.length; ++j) { ++numComp; if (a[j].compareTo(a[p]) < 0) { p = j; ++numAsgn; } } T temp = a[i]; a[i] = a[p]; a[p] = temp; ++numAsgn; //if (tracing == 'Y') { System.out.println("one more selected: " + Arrays.toString(a)); //} } }

/** * Perform shell sort on the given array. * * @param a the array to sort. */ public static > void shellSort(T[] a) { int gap = a.length / 2; ++numComp; while (gap >= 1) {

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

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

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

2. How were various roles filled?

Answered: 1 week ago

Question

2. What process will you put in place to address conflicts?

Answered: 1 week ago