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.
It must produce a comparison chart of all implemented sort algorithms (consider both comparisons and exchanges)
(So must show how many times its compared an exchanged (swapped).
. Start with insertion, selection and bubble sorts (other sorts will be included later.
This is for Data and Algorithm. Plese, help I will upvote. I promise!
Please include this
public class BubbleSortingMain {
public static void main(String[] args) {
int array1[] =
{1,2,3,4,5,6,7,8,9,10}; // best case
int array2[]=
{10,9,8,7,6,5,4,3,2,1};//worst case
bubbleSort(array1);
bubbleSort(array2);
}
public static void bubbleSort(int[] array) {
int numberOfItems = array.length;
int comp=0,exch=0,temp=0;
boolean cont = true;
for (int pass=1; pass != numberOfItems;
pass++) {
if (cont) {
cont = false;
for (int index=0; index != numberOfItems-pass; index++) {
comp++;
if (array[index]>
array[index+1]) {
exch++;
temp = array[index];
array[index] =
array[index+1];
array[index+1] = temp;
cont = true;
}
}
}
else
break; // end outer if
}
System.out.println ("comparsion: " + comp); // end
inner if
System.out.println ("exchange: " + exch); // end inner
for
}
}
pseudocode in code comments :
public class Sorting {
public static void main(String[] args) {
int array1[] = {1,2,3,4,5,6,7,8,9,10}; // best case
int array2[]= {10,9,8,7,6,5,4,3,2,1};//worst case
bubbleSort(array1);
bubbleSort(array2);
int array3[] = {1,2,3,4,5,6,7,8,9,10}; // best case
int array4[]= {10,9,8,7,6,5,4,3,2,1};//worst case
selectionSort(array3);
selectionSort(array4);
int array5[] = {1,2,3,4,5,6,7,8,9,10}; // best case
int array6[]= {10,9,8,7,6,5,4,3,2,1};//worst case
insertionSort(array5);
insertionSort(array6);
}
public static void printArrray(int[] array){
int numberOfItems = array.length;
for(int i=0;i<numberOfItems;i++){
System.out.print(array[i] + " ");
}
System.out.println();
}
/*
In insertion sort we make space for the current element in the array , and then insert the current element at that position
ALGORITHM :
1.Loop until pass = 1 to number of items
2.let key be the current element
3.For each element left to the current element , if it is greater than curr element , we exchange pass and index
4.Thus at the end of loop we ll be left with a position suitable for key
5.We insrt key into that position
*/
public static void insertionSort(int[] array){
System.out.println("********* INSERTION SORT ************");
printArrray(array);
int numberOfItems = array.length;
int comp=0,exch=0;
for (int pass = 1; pass < numberOfItems; ++pass) {
int key = array[pass];
int index = pass - 1;
comp+=index;
while (index >= 0 && array[index] > key) {
exch++;
array[index+1] = array[index];
index = index - 1;
}
exch++;
array[index+1] = key;
}
System.out.println ("comparsion: " + comp);
System.out.println ("exchange: " + exch);
printArrray(array);
System.out.println();
}
/*
In selection sort we select an element and find the minimum element on the right of that element
and then exchange the element with the selected element.
Hence at any stage of selection sort the left of selected element will always be sorted
ALGORITHM :
1.LOOP from (0 to length of array-1)
2.let curr be the index current element
store curr in temp variable minI
3.loop from curr to the end of array to find minium number
if(array[index]<array[minI]
minI = index
4.After inner loop exchange elements at curr and minI
*/
public static void selectionSort(int[] array){
System.out.println("********** SELECTION SORT ***********");
printArrray(array);
int numberOfItems = array.length;
int comp=0,exch=0;
for (int pass=0; pass<numberOfItems-1; pass++)
{
int minI = pass;
for (int index = pass+1; index < numberOfItems; index++) {
comp++;
if (array[index] < array[minI])
minI = index;
}
exch++;
int temp = array[minI];
array[minI] = array[pass];
array[pass] = temp;
}
System.out.println ("comparsion: " + comp);
System.out.println ("exchange: " + exch);
printArrray(array);
System.out.println();
}
public static void bubbleSort(int[] array) {
System.out.println("********* BUBBLE SORT ***********");
printArrray(array);
int numberOfItems = array.length;
int comp=0,exch=0,temp=0;
boolean cont = true;
for (int pass=1; pass != numberOfItems; pass++) {
if (cont) {
cont = false;
for (int index=0; index != numberOfItems-pass; index++) {
comp++;
if (array[index]> array[index+1]) {
exch++;
temp = array[index];
array[index] = array[index+1];
array[index+1] = temp;
cont = true;
}
}
}
else
break; // end outer if
}
System.out.println ("comparsion: " + comp); // end inner if
System.out.println ("exchange: " + exch); // end inner for
printArrray(array);
System.out.println();
}
}






Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...
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...
Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
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....
Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...
Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...
Below I have done a bubble sort on an array. I need to figure out how to change the --- bubbleSort() method... with a new method called oddEvenSort() method.. I know I have to do two passes one for odd, one for even, can I have someone help me do this please I keep getting a lot of errors when I attempt to do it. Java code to change is below. I have to sort the array using one pass...
Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public void sortImprovement1(int[] numbers) { for (int i = 1; i < numbers.length; i++) { for (int j = 0; j < numbers.length - i; j++) { if (numbers[j] > numbers[j + 1]) { swap(numbers, j, j + 1); } } } } Snippet 2.3: Bubble sort improvement 1 Change the bubble sort method so that it stops sorting if the array is untouched after...
Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner; public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1) bubbleSort(array); if (ch==2) insertion(array); if (ch==3) Selection(array); if (ch==4) break; print(array); System.out.println(); } }...
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...