In: Computer Science
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 :)
//------------- 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