Question: Please answer this in java. I have already started (implemented the sorting methods and implemented the array). Please give the answer as an extension to
Please answer this in java. I have already started (implemented the sorting methods and implemented the array). Please give the answer as an extension to my current code, as this is the way we were taught and I cannot turn something in that has pieces of codes we have learned. Thank You
Write a program that implements the following sorting algorithms:
Selection Sort
Bubble Sort
Merge Sort
Quick Sort
Next test the efficiency of each using the following steps:
Create an array of randomly selected whole numbers of 1,000 elements
Each value is selected from the range 0-999
Sort that array using each method
Make sure to copy the initial array into a new one so that the tests will be accurate and not be sorting sorted arrays
Show that each method successfully sorted the array
Count the number of checks each method had to perform before fully sorting the array
Keep track of the total number of checks for each algorithm
Perform this 20 times and keep track of the running average
Finally at the end give the average number of checks using each algorithm.
import java.util.Arrays; import java.util.Random;
public class SortingComparison { static Random r = new Random(); public static void main(String[] args) { // TODO Auto-generated method stub int[] array = createArray(); selectionSort(array); bubbleSort(array); mergeSort(array); quickSort(array,0,array.length); } private static int[] createArray() {//sorts the array int size = r.nextInt(1000); int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = r.nextInt(1000); } Arrays.sort(array); return array; } public static int[] selectionSort(int[] a) { int smallestIndex=0; for(int i=0;i
public static int[] bubbleSort(int[] a) { boolean done = false; int n = a.length; while(done == false) { done=true; for(int i=0;i
//populate left array for(int i=0;i int pivot=a[(left+right)/2]; while(i<=j) { while(a[i] } while(a[j]>pivot) { j--;//correct position } if(i<=j) { int temp =a[i]; a[i]=a[j]; a[j]=temp; i++; j--; } } return i; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
