Question

In: Computer Science

Ex. Create a GUI program to fill in the text field the performance time if each...

Ex. Create a GUI program to fill in the text field the performance time if each alignment method is used for 50,000 random integers.

Each sort method is indicated by a button:Selection Sort ,Bubble Sort ,Insertion Sort ,Mergesort ,Quicksort ,Counting Sort ,Radix Sort ,Heapsort

Write java code and show me the output.

Thank you :)

Solutions

Expert Solution

//------------- Sorts.java -----------------

import java.util.Arrays;

public class Sorts {
   // selection sort
   public void selectionSort(int[] array) {
       int len = array.length;
       int index;
       for (int i = 0; i < len - 1; i++) {
           // Find the minimum element in unsorted array
           index = i;
           for (int j = i + 1; j < len; j++)
               if (array[j] < array[index]) {
                   index = j;
               }
           // Swap the found minimum element with the first
           // element
           int temp = array[index];
           array[index] = array[i];
           array[i] = temp;
       }
   }

   // Bubble Sort
   public void bubbleSort(int[] array) {
       int len = array.length;
       for (int i = 0; i < len - 1; i++) {
           for (int j = 0; j < len - i - 1; j++)
               if (array[j] > array[j + 1]) {
                   int temp = array[j];
                   array[j] = array[j + 1];
                   array[j + 1] = temp;
               }
       }
   }

   // Insertion Sort
   public void insertionSort(int[] array) {
       int len = array.length;
       for (int i = 1; i < len; ++i) {
           int val = array[i];
           int j = i - 1;
           while (j >= 0 && array[j] > val) {
               array[j + 1] = array[j];
               j = j - 1;
           }
           array[j + 1] = val;
       }
   }

   // Mergesort
   void merge(int[] array, int l, int m, int r) {
       int lenLeft = m - l + 1;
       int lenRight = r - m;

       int[] leftArray = new int[lenLeft];
       int[] rightArray = new int[lenRight];

       for (int i = 0; i < lenLeft; ++i)
           leftArray[i] = array[l + i];
       for (int j = 0; j < lenRight; ++j)
           rightArray[j] = array[m + 1 + j];

       int i = 0, j = 0;

       int k = l;
       while (i < lenLeft && j < lenRight) {
           if (leftArray[i] <= rightArray[j]) {
               array[k] = leftArray[i];
               i++;
           } else {
               array[k] = rightArray[j];
               j++;
           }
           k++;
       }

       while (i < lenLeft) {
           array[k] = leftArray[i];
           i++;
           k++;
       }

       while (j < lenRight) {
           array[k] = rightArray[j];
           j++;
           k++;
       }
   }

   void sort(int[] array, int l, int r) {
       if (l < r) {
           // Find the middle point
           int m = (l + r) / 2;

           // Sort first and second halves
           sort(array, l, m);
           sort(array, m + 1, r);

           // Merge the sorted halves
           merge(array, l, m, r);
       }
   }

   public void mergeSort(int[] array) {

       sort(array, 0, array.length - 1);

   }

   // Quick Sort
   int partition(int[] array, int low, int high) {
       int pivot = array[high];
       int i = (low - 1);
       for (int j = low; j < high; j++) {
           if (array[j] < pivot) {
               i++;
               int temp = array[i];
               array[i] = array[j];
               array[j] = temp;
           }
       }
       int temp = array[i + 1];
       array[i + 1] = array[high];
       array[high] = temp;
       return i + 1;
   }

   void quickSort(int[] array, int low, int high) {
       if (low < high) {
           int pivot = partition(array, low, high);

           sort(array, low, pivot - 1);
           sort(array, pivot + 1, high);
       }
   }

   // Counting Sort
   public void countSort(int[] array) {
       int max = Arrays.stream(array).max().getAsInt();
       int min = Arrays.stream(array).min().getAsInt();
       int range = max - min + 1;
       int[] numCount = new int[range];
       int[] sortOutput = new int[array.length];
       for (int i = 0; i < array.length; i++) {
           numCount[array[i] - min]++;
       }

       for (int i = 1; i < numCount.length; i++) {
           numCount[i] += numCount[i - 1];
       }

       for (int i = array.length - 1; i >= 0; i--) {
           sortOutput[numCount[array[i] - min] - 1] = array[i];
           numCount[array[i] - min]--;
       }
       for (int i = 0; i < array.length; i++) {
           array[i] = sortOutput[i];
       }
   }

   public void countSortExp(int[] array, int n, int exp) {
       int[] output = new int[n];
       int i;
       int[] count = new int[10];
       for (i = 0; i < count.length; i++) {
           count[i] = 0;
       }
       for (i = 0; i < n; i++)
           count[(array[i] / exp) % 10]++;
       for (i = 1; i < 10; i++)
           count[i] += count[i - 1];

       for (i = n - 1; i >= 0; i--) {
           output[count[(array[i] / exp) % 10] - 1] = array[i];
           count[(array[i] / exp) % 10]--;
       }

       for (i = 0; i < n; i++)
           array[i] = output[i];
   }

   // Radix Sort
   public void radixSort(int[] array) {
       int max = Arrays.stream(array).max().getAsInt();
       int n = array.length;
       for (int exp = 1; max / exp > 0; exp *= 10)
           countSortExp(array, n, exp);
   }

   // Heap Sort
   public void heapSort(int[] array) {
       int n = array.length;
       for (int i = n / 2 - 1; i >= 0; i--)
           heapify(array, n, i);
       int temp;
       for (int i = n - 1; i >= 0; i--) {
           temp = array[0];
           array[0] = array[i];
           array[i] = temp;
           heapify(array, i, 0);
       }
   }
   void heapify(int array[], int n, int i) {
       int largest = i;
       int left = 2 * i + 1;
       int right = 2 * i + 2; // right = 2*i + 2

       // If left child is larger than root
       if (left < n && array[left] > array[largest])
           largest = left;

       // If right child is larger than largest so far
       if (right < n && array[right] > array[largest])
           largest = right;

       // If largest is not root
       if (largest != i) {
           int swap = array[i];
           array[i] = array[largest];
           array[largest] = swap;

           // Recursively heapify the affected sub-tree
           heapify(array, n, largest);
       }
   }
}

//=---------------- Main.java ---------------

import java.util.Arrays;
import java.util.Random;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.awt.*;

public class Main extends JFrame implements ActionListener {
   JButton selectionSort;
   JButton bubbleSort;
   JButton insertionSort;
   JButton mergeSort;
   JButton quickSort;
   JButton countingSort;
   JButton radixSort;
   JButton heapSort;
   JTextField output;
   JLabel prompt;
   JPanel panel;
   JPanel panel2;
   Sorts sorts;
   int[] array;
   final int SIZE = 50000;
   Random random = new Random();
   NumberFormat formatter = new DecimalFormat("#0.00000000");
   JLabel sortName;

   Main() {
       setSize(500, 500);
       setLayout(new BorderLayout());

       sorts = new Sorts();
       array = new int[SIZE];
       initializeArray();

       panel = new JPanel(new GridLayout(2, 4, 10, 10));
       panel2 = new JPanel(null);
       selectionSort = new JButton("Selection Sort");
       bubbleSort = new JButton("Bubble Sort");
       insertionSort = new JButton("Insertion Sort");
       mergeSort = new JButton("Merge Sort");
       quickSort = new JButton("Quick Sort");
       countingSort = new JButton("Couting Sort");
       radixSort = new JButton("Radix Sort");
       heapSort = new JButton("Heap Sort");

       prompt = new JLabel("Time Taken in seconds to sort 50,000 random integers: ");
       sortName = new JLabel("Sort Name: ");
       output = new JTextField(20);

       panel.add(selectionSort);
       panel.add(bubbleSort);
       panel.add(insertionSort);
       panel.add(mergeSort);
       panel.add(quickSort);
       panel.add(countingSort);
       panel.add(radixSort);
       panel.add(heapSort);

       prompt.setBounds(50, 10, 400, 30);
       panel2.add(prompt);

       sortName.setBounds(10, 80, 200, 30);
       panel2.add(sortName);
       output.setBounds(200, 80, 250, 50);
       panel2.add(output);
       output.setEnabled(false);

       output.setFont(new Font("SansSerif", Font.BOLD, 20));
       output.setHorizontalAlignment(JTextField.CENTER);

       add(panel2);
       add(panel, BorderLayout.SOUTH);

       selectionSort.addActionListener(this);
       bubbleSort.addActionListener(this);
       insertionSort.addActionListener(this);
       mergeSort.addActionListener(this);
       quickSort.addActionListener(this);
       countingSort.addActionListener(this);
       radixSort.addActionListener(this);
       heapSort.addActionListener(this);

   }

   public void initializeArray() {
       for (int i = 0; i < SIZE; i++) {
           array[i] = random.nextInt(10000);
       }
   }

   public int[] copyArray() {
       return Arrays.copyOf(array, SIZE);
   }

   public void actionPerformed(ActionEvent ae) {
       if (ae.getSource() == selectionSort) {

           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Selection Sort ");
           sorts.selectionSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == bubbleSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Bubble Sort ");
           sorts.bubbleSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == insertionSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Insertion Sort ");
           sorts.insertionSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == mergeSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Merge Sort ");
           sorts.mergeSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == quickSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Quick Sort ");
           sorts.quickSort(sortArray, 0, sortArray.length - 1);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == countingSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Counting Sort ");
           sorts.countSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == radixSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Radix Sort ");
           sorts.radixSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       } else if (ae.getSource() == heapSort) {
           int[] sortArray = copyArray();
           long start = System.currentTimeMillis();
           sortName.setText("Sort Name: Heap Sort ");
           sorts.heapSort(sortArray);
           long end = System.currentTimeMillis();
           output.setText(formatter.format((end - start) / 1000d) + " seconds");

       }

   }

   public static void main(String[] args) {
       Main main = new Main();
       main.setVisible(true);
       main.setTitle("Soring Performance Calculator");
       main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

//OUTPUTS


Related Solutions

Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Create a UI that has a field in which to enter text, a field to display...
Create a UI that has a field in which to enter text, a field to display read-only text and a button. When the button is pressed, take the value of the text box and try to create a month object. If a number was entered, use the constructor that accepts an integer. If alphabetic characters were entered, use the constructor that accepts a string. If nothing is entered, use the no-argument constructor. If a valid month object is created, use...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt)...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Write a GUI-based program that allows the user to open, edit, and save text files. The...
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and...
Swift - Implement a simple text editor program that includes a text input field (with several...
Swift - Implement a simple text editor program that includes a text input field (with several lines), a text label, and two buttons, called "Open" and "Save". When the button "Save" is touched, the app will save the contents of the text input field to a data file (sample.txt). When the button "Open" is touched, the app will read the content from the data file, (sample.txt)) and feed the content into the text label. Thank you.
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
ou will create a GUI data entry program to be used to enter Customer Information. This...
ou will create a GUI data entry program to be used to enter Customer Information. This program should be based on the requirements and design you created in previous modules. It will be a GUI, based either on the Java Swing classes or JavaFX. The program will allow for entry of all fields described in your requirements and include appropriate validations for each field. It will be left to you to decide if the validations methods should be part of...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
Fill in the blank. In a drive thru performance study, the average service time for McDonald's...
Fill in the blank. In a drive thru performance study, the average service time for McDonald's is 203.21 seconds with a standard deviation of 5.67 seconds. A random sample of 90 times is taken. There is a 51% chance that the average drive-thru service time is less than ________ seconds. 1) 203.22 2) There is not enough information to determine this. 3) 203.2 4) 203.07 5) 203.35
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT