You are on page 1of 4

/* * This program compares benchmarks for Selectio n Sort, * Insertion Sort, and Mergesort (and Arrays.

sor t for * Question 16 in the exercises) */ import import import import import java.awt.*; java.awt.event.*; javax.swing.*; java.util.Random; java.util.Arrays;

public class Benchmarks extends JFrame { private static int numberOfRuns = 20; private JTextField arraySizeInput, display; private String sortMethodNames[] = {"Selection Sort", "Insertion Sort", "Merge sort", "Quicksort", "Arrays.sort"}; private JComboBox chooseSortMethod; private final long seed; private int arraySize; // Constructor public Benchmarks() { super("Benchmarks"); Container c = getContentPane(); c.setLayout(new GridLayout(6, 1)); c.add(new JLabel(" Array size: ")); arraySizeInput = new JTextField(4); arraySizeInput.setText("1000"); arraySizeInput.selectAll(); c.add(arraySizeInput); chooseSortMethod = new JComboBox(sortMethodN ames); c.add(chooseSortMethod); JButton run = new JButton("Run"); run.addActionListener(new RunButtonListener( )); c.add(run); c.add(new JLabel(" Avg Time (milliseconds): ")); display = new JTextField(" Ready"); display.setBackground(Color.YELLOW); display.setEditable(false); c.add(display); // Use the same random number generator seed for all benchmarks

// in one run of this program: seed = System.currentTimeMillis(); } /* * Fills a[] with random numbers and sorts it using the sorting method * specified in sortMethod: * 1 -- Selection Sort * 2 -- Insertion Sort * 3 -- Mergesort * 4 -- Quicksort * 5 -- java.util.Arrays.sort * This is repeated numberOfRuns times for bet ter accuracy * Returns the total time it took in milliseco nds. */ private long runSort(double[] a, int sortMetho d, int numberOfRuns) { Random generator = new Random(seed); long difference = 0, num = 0; for (int k = 0; numberOfRuns > 0; numberOfRun s--){ a[k] = generator.nextDouble(); if(sortMethod ==1){ num += System.currentTimeMill is(); SelectSort.sort(a); difference += (System.current TimeMillis()); } if(sortMethod ==2){ num += System.currentTimeMill is(); InsertSort.sort(a); difference += (System.current TimeMillis()); } if(sortMethod ==3){ num += System.currentTimeMill is(); MergeSort.sort(a); difference += (System.current TimeMillis()); } if(sortMethod ==4){ num += System.currentTimeMill is(); QuickSort.sort(a); difference += (System.current TimeMillis()); } if(sortMethod ==5){ num += System.currentTimeMill is(); java.util.Arrays.sort(a); difference += (System.current TimeMillis());

} } long numbah = difference - num; return numbah; } /* * Handles Run button events */ private class RunButtonListener implements Act ionListener { public void actionPerformed(ActionEvent e) { String inputStr = arraySizeInput.getText() .trim(); try { arraySize = Integer.parseInt(inputStr); } catch (NumberFormatException ex) { display.setText(" Invalid array size"); arraySize = 0; return; } if (arraySize <= 0) { display.setText(" Invalid array size"); return; } if (arraySize <= 0) return; int sortMethod = chooseSortMethod.getSelec tedIndex() + 1; double a[] = new double[arraySize]; double avgTime = (double)runSort(a, sortMe thod, numberOfRuns) / numberOfRuns; display.setText(String.format(" %.2f", av gTime)); arraySizeInput.selectAll(); arraySizeInput.requestFocus(); System.out.println("Array size = " + array Size + " Runs = " + numberOfRuns + " " + sortMethodNames[sortMethod - 1] + " avg time: " + avgTime); } } //******************************************** ****************

public static void main(String[] args) { numberOfRuns = 20; if (args.length > 0) { int n = -1; try { n = Integer.parseInt(args[0].trim()); } catch (NumberFormatException ex) { System.out.println("Invalid command-line parameter"); System.exit(1); } if (n > 0) numberOfRuns = n; } Benchmarks window = new Benchmarks(); window.setBounds(300, 300, 180, 200); window.setDefaultCloseOperation(EXIT_ON_CLOS E); window.setVisible(true); } }

You might also like