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
Next, write a class Sorter with the following
Example Output:
Cylinder Sorter!
Sorted correctly? true
It took 313ms for bubble sort
Sorted correctly? true
It took 15ms for merge sort
Sorted correctly? true
It took 16ms for heap sort
Driver:
import java.util.*; public class SortingTester { public static void main(String[] args) { System.out.println("Cylinder Sorter!"); Cylinder[] org = makeArray(); Cylinder[] a = org.clone(); long beforeTime = System.currentTimeMillis(); Sorter.bubbleSort(a); long afterTime = System.currentTimeMillis(); System.out.println("Sorted correctly? "+isSortedCorrectly(a)); System.out.println("It took "+(afterTime-beforeTime)+"ms for bubble sort"); a = org.clone(); beforeTime = System.currentTimeMillis(); Sorter.mergeSort(a); afterTime = System.currentTimeMillis(); System.out.println("Sorted correctly? "+isSortedCorrectly(a)); System.out.println("It took "+(afterTime-beforeTime)+"ms for merge sort"); a = org.clone(); beforeTime = System.currentTimeMillis(); Sorter.heapSort(a); afterTime = System.currentTimeMillis(); System.out.println("Sorted correctly? "+isSortedCorrectly(a)); System.out.println("It took "+(afterTime-beforeTime)+"ms for heap sort"); } //Checks to see if array a has been sorted correctly public static boolean isSortedCorrectly(Cylinder[] a) { for(int i=0;ia[i+1].getVolume()) return false; } return true; } //Creates an array of random cylinders public static Cylinder[] makeArray() { Cylinder[] a = new Cylinder[10000];//A big ole array Random r = new Random(100);//fixes the seed at 100 for(int i=0;i
NOTE: Please upvote if you find this solution useful in any way.
For any doubts or queries, use the comment section below.
NOTE:- I have just taken some random array value instead of cylinder volumes. You can replace the array with your cylinder volumes array.
CODE:
import java.util.*;
class Sorter{
void bubbleSort(int arr[]){
int n = arr.length;
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (arr[j] > arr[j+1]){
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void mergeSort(int arr[], int l, int r){
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void heapSort(int arr[]){
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
}
class cylindersorter {
public static void main(String[] args) {
System.out.println("Cylinder Sorter!");
Sorter sorter = new Sorter();
Random rd = new Random();
int[] array = new int[500];
for (int i = 0; i < array.length; i++)
array[i] = rd.nextInt(); // storing random integers in
an array
int[] a = array.clone();
long beforeTime = System.currentTimeMillis();
sorter.bubbleSort(a);
long afterTime = System.currentTimeMillis();
System.out.println("Sorted
correctly? "+isSortedCorrectly(a));
System.out.println("It took "+(afterTime-beforeTime)+"ms for bubble
sort");
a = array.clone();
beforeTime = System.currentTimeMillis();
sorter.mergeSort(a,0,a.length-1);
afterTime = System.currentTimeMillis();
System.out.println("Sorted correctly?
"+isSortedCorrectly(a));
System.out.println("It took "+(afterTime-beforeTime)+"ms for merge
sort");
a = array.clone();
beforeTime = System.currentTimeMillis();
sorter.heapSort(a);
afterTime = System.currentTimeMillis();
System.out.println("Sorted correctly?
"+isSortedCorrectly(a));
System.out.println("It took "+(afterTime-beforeTime)+"ms for heap
sort");
}
public static boolean isSortedCorrectly(int[] a){
for(int i=0;i<a.length-1;i++){
if(a[i] > a[i+1])
return false;
}
return true;
}
}
ScreenShots:
Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...
A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...
I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....
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....
I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...
The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...
I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...
//Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...
Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...
How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and compare with a graph? I have these methods: //This one receives one size(n) parameter public static ArrayList<Integer> RandomArray(int n){ ArrayList<Integer> randomArray = new ArrayList<Integer>(n); Random rand = new Random(); //--- random number rand.setSeed(System.currentTimeMillis()); for(int i = 0; i < n; i++ ) { //loop for creating...