Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to write a bench mark that writes a file with these markers andomly generate data to pass to the sorting methods. It

I am trying to write a bench mark that writes a file with these markers andomly generate data to pass to the sorting methods. It should produce 40 data sets for each value of n, the size of the data set and average the result of those 40 runs. The exact same data must be used for both sorting algorithms. It should also create 12 different sizes of data sets. Choose sizes that will clearly demonstrate the trend as n becomes large. Be sure that the data set sizes are evenly spaced Here is the code I have so far It uses a merge and quick sort classes:

import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.Random; import java.util.stream.IntStream; import javax.swing.*; import javax.swing.table.DefaultTableModel;

import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

public class Report {

private static final int NUM_OF_DATASET_SIZES = 12; private static final int NUM_OF_RUNS = 40;

public static void main(String[] args) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Select the input file"); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); Scanner scanner; try { scanner = new Scanner(selectedFile); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not found", "Error", JOptionPane.ERROR_MESSAGE); return; } int[] datasetSizes = new int[NUM_OF_DATASET_SIZES]; int[][] counts = new int[NUM_OF_DATASET_SIZES][NUM_OF_RUNS]; long[][] times = new long[NUM_OF_DATASET_SIZES][NUM_OF_RUNS]; for (int i = 0; i < NUM_OF_DATASET_SIZES; i++) { datasetSizes[i] = scanner.nextInt(); for (int j = 0; j < NUM_OF_RUNS; j++) { counts[i][j] = scanner.nextInt(); times[i][j] = scanner.nextLong(); } } DefaultTableModel model = new DefaultTableModel(new Object[]{"Data set size", "Average count", "Count CV(%)", "Average time", "Time CV(%)"}, 0); for (int i = 0; i < NUM_OF_DATASET_SIZES; i++) { int sumCount = 0; long sumTime = 0; for (int j = 0; j < NUM_OF_RUNS; j++) { sumCount += counts[i][j]; sumTime += times[i][j]; } double avgCount = (double) sumCount / NUM_OF_RUNS; double avgTime = (double) sumTime / NUM_OF_RUNS; double countCV = 0; double timeCV = 0; for (int j = 0; j < NUM_OF_RUNS; j++) { countCV += Math.pow(counts[i][j] - avgCount, 2); timeCV += Math.pow(times[i][j] - avgTime, 2); } countCV = Math.sqrt(countCV / NUM_OF_RUNS) / avgCount * 100; timeCV = Math.sqrt(timeCV / NUM_OF_RUNS) / avgCount * 100; } } } }

//ApstractSort.java

import java.util.Random;

abstract class AbstractSort { int count = 0; long startTime, endTime;

abstract void sort(int[] data);

void startSort() { count = 0; startTime = System.nanoTime(); }

void endSort() { endTime = System.nanoTime(); }

void incrementCount() { count++; }

int getCount() { return count; }

long getTime() { return endTime - startTime; } } //Mergesort.java

class MergeSort extends AbstractSort { void sort(int[] data) { int n = data.length; if (n < 2) { return; } int mid = n / 2; int[] left = new int[mid]; int[] right = new int[n - mid]; for (int i = 0; i < mid; i++) { left[i] = data[i]; } for (int i = mid; i < n; i++) { right[i - mid] = data[i]; } sort(left); sort(right); merge(left, right, data); }

private void merge(int[] left, int[] right, int[] data) { int i = 0, j = 0, k = 0; while (i < left.length && j < right.length) { incrementCount(); if (left[i] <= right[j]) { data[k++] = left[i++]; } else { data[k++] = right[j++]; } } while (i < left.length) { data[k++] = left[i++]; } while (j < right.length) { data[k++] = right[j++]; } } }

//QuickSort.java

class QuickSort extends AbstractSort { void sort(int[] data) { quickSort(data, 0, data.length - 1); }

private void quickSort(int[] data, int low, int high) { if (low < high) { int pivotIndex = partition(data, low, high); quickSort(data, low, pivotIndex - 1); quickSort(data, pivotIndex + 1, high); } }

private int partition(int[] data, int low, int high) { int pivot = data[high]; int i = low - 1; for (int j = low; j < high; j++) { incrementCount(); if (data[j] <= pivot) { i++; swap(data, i, j); } } swap(data, i + 1, high); return i + 1; }

private void swap(int[] data, int i, int j) { int temp = data[i]; data[i] = data[j]; data[j] = temp; } }

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_2

Step: 3

blur-text-image_3

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 Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions

Question

(4) C (U + 2102), the set of complex numbers; and

Answered: 1 week ago

Question

4. How has e-commerce affected business-to-business transactions?

Answered: 1 week ago