Java
(Determining Big O) write a PerformanceTest class and compare
the performance of selection and bubblesort. Use the following
“PerfomanceTest” example. Instead of a simple loop, use the
mentioned sort algorithms.
⦁ Test with a sorted array
⦁ Test with an unsorted array (you can create the
unsorted array randomly and then use the same one for both of the
algorithms)
Program:
PerformanceTest.java:
//header files
import java.util.Random;
public class PerformanceTest {
// generate list of integers as static
static int[][] list = new int[2][];
static int[] list1 = new int[10];
static int[] list2 = new int[10];
// main function
public static void main(String[] args) {
// create a string of number say that holds the names of the sort
String algorithm[] = { "Selection Sort", "Bubble Sort"};
String size[] = { "10", "10"};
// call the display method to inform that which is hold which type of
// numbers
displayList();
// generate random numbers into the respective lists
generatelist();
// assign all the list's into the list of 2-D number say
list[0] = list1;
list[1] = list2;
//display the values in the list
for (int i = 0; i < list.length; i++)
{
System.out.println();
System.out.println("List size : " + size[i]);
for(int k=0;k<list[i].length;k++)
{
if(k%10==0)
System.out.println();
System.out.print(list[i][k]+" ");
}
System.out.println();
for (int j = 0; j < 2; j++) {
System.out.println("\nTrail : " + (j + 1));
System.out.print("The Elapsed time of " + algorithm[0]
+ " algorithm for " + size[i] + " items is = ");
SelectionSortTimingForAllList(list[i]);
System.out.print("The Elapsed time of " + algorithm[1]
+ " algorithm for " + size[i] + " items is = ");
BubbleSortTimingForAllList(list[i]);
}
}
}
// Method to generate the random numbers with in the given range
public static void generatelist()
{
list1 = generateRandomNumbers(10, 10);
list2 = generateSortedNumbers(10, 10);
}
// generateRandomNumbers method will generate the random numbers
// of the given range into the respective size of lists
public static int[] generateRandomNumbers(int length, int range) {
int[] randNums = new int[length];
Random rand = new Random();
for (int i = 0; i < length; ++i) {
randNums[i] = rand.nextInt(range);
}
return randNums;
}
// generateSortedNumbers method will generate the random numbers
// of the given range into the respective size of lists
public static int[] generateSortedNumbers(int length, int range) {
int[] randNums = new int[length];
Random rand = new Random();
for (int i = 0; i < length; ++i) {
randNums[i] = i+1;
}
return randNums;
}
// displayList will display the menu
public static void displayList()
{
System.out.println("1. List of 10 numbers.");
System.out.println("2. List of 10 numbers.");
}
// SelectionSortTimingForAllList method will contain a list to which
// selection sort method is called and the respective list run time is
// calculated
public static void SelectionSortTimingForAllList(int[] list)
{
StopWatch timer = new StopWatch();
timer.start();
selectionSort(list);
timer.stop();
System.out.println(timer.getElapsedTime() + " nanoseconds");
}
// BubbleSortTimingForAllList method will contain a list to which
// BubbleSort method is called and the respective list run time
// is calculated
public static void BubbleSortTimingForAllList(int[] list)
{
StopWatch timer = new StopWatch();
timer.start();
BubbleSort(list);
timer.stop();
System.out.println(timer.getElapsedTime() + " nanoseconds");
}
// Selection sort code
public static int[] selectionSort(int[] numbers) {
for (int i = 0; i < numbers.length - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < numbers.length; ++j) {
if (numbers[j] < numbers[minIndex])
minIndex = j;
}
swap(numbers, minIndex, i);
}
return numbers;
}
// Bubble Sort code
public static int[] BubbleSort(int[] numbers)
{
int size=numbers.length;
int k;
for (int i = size; i>=0; i--)
{
for (int j = 0 ;j < size-1; j++)
{
k = j+1;
if (numbers[j] < numbers[k])
{
swap(numbers, j, k);
}
else
{
break;
}
}
}
return numbers;
}
private static void swap(int[] numbers, int i, int j)
{
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
StopWatch.java:
import java.util.*;
import java.lang.*;
public class StopWatch
{
private long elapsedTime;
private long startTime;
private boolean isRunning;
// constructor that will sets the time to 0
public StopWatch()
{
reset();
}
// The start method will starts the stop watch and time starts accumulating
// now.
public void start()
{
if (isRunning)
return;
isRunning = true;
startTime = System.nanoTime();
}
// The stop method will stops the stop watch
// the stop time is added to the elapsed time
public void stop()
{
if (!isRunning)
return;
isRunning = false;
long endTime = System.nanoTime();
elapsedTime = elapsedTime + endTime - startTime;
}
// The getElapsedTime method return the elapsed time
public long getElapsedTime()
{
if (isRunning)
{
long endTime = System.nanoTime();
return elapsedTime + endTime - startTime;
}
else
return elapsedTime;
}
// The reset method will resets the elapsedTime to 0
public void reset()
{
elapsedTime = 0;
isRunning = false;
}
}
Sample Output:
Java (Determining Big O) write a PerformanceTest class and compare the performance of selection and bubblesort....
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...
Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) { int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...
This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...
The objective of this Assignment is to compare the performance of some standard SORT algorithms. You should look at MergeSort, QuickSort, and Insertion Sort; you can also look at an additional Sort mechanism that isn't covered in class or that isn't covered in the book for extra credit. You will have to find the right implementations in Java so that you can make your comparisons. Things you will be comparing are the raw running time and the time complexity in...
1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...
Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...
Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....