Question: Could someone fix the code below to print out the right output in the picture Heres the main code: import java.util.Arrays; import java.util.Random; public class

Could someone fix the code below to print out the right output in the picture

Heres the main code:

import java.util.Arrays; import java.util.Random; public class Main { private static final int N = 20000; private static final int RANGE = 150; private static final Random random = new Random(); public static void main(String[] args) { Container[] containers = generateContainers(N, RANGE); Container[] counting = Arrays.copyOf(containers, containers.length); Container[] cocktail = Arrays.copyOf(containers, containers.length); Container[] quick = Arrays.copyOf(containers, containers.length); long startTime, endTime; startTime = System.currentTimeMillis(); SortAlgs.countingSort(counting); endTime = System.currentTimeMillis(); System.out.printf("Counting took %d ms ", endTime - startTime); startTime = System.currentTimeMillis(); SortAlgs.cocktailSort(cocktail); endTime = System.currentTimeMillis(); System.out.printf("Cocktail took %d ms ", endTime - startTime); startTime = System.currentTimeMillis(); SortAlgs.quickSort(quick); endTime = System.currentTimeMillis(); System.out.printf("Quick took %d ms ", endTime - startTime); } private static Container[] generateContainers(int n, int range) { Container[] containers = new Container[n]; for (int i = 0; i < n; i++) { int key = random.nextInt(range + 1); containers[i] = new Container(key); } return containers; } } 

Heres the SortAlgs code:

public class SortAlgs { public static void cocktailSort(Container[] array) { boolean swapped = true; int start = 0; int end = array.length; while (swapped) { swapped = false; for (int i = start; i < end - 1; ++i) { if (array[i].getKey() > array[i + 1].getKey()) { Container temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; swapped = true; } } if (!swapped) { break; } swapped = false; for (int i = end - 1; i >= start + 1; --i) { if (array[i].getKey() < array[i - 1].getKey()) { Container temp = array[i]; array[i] = array[i - 1]; array[i - 1] = temp; swapped = true; } } start++; end--; } } public static void quickSort(Container[] array) { quickSort(array, 0, array.length - 1); } private static void quickSort(Container[] array, int left, int right) { if (left < right) { int partitionIndex = partition(array, left, right); quickSort(array, left, partitionIndex - 1); quickSort(array, partitionIndex + 1, right); } } private static int partition(Container[] array, int left, int right) { int pivot = array[right].getKey(); int i = left - 1; for (int j = left; j < right; j++) { if (array[j].getKey() <= pivot) { i++; Container temp = array[i]; array[i] = array[j]; array[j] = temp; } } Container temp = array[i + 1]; array[i + 1] = array[right]; array[right] = temp; return i + 1; } public static void countingSort(Container[] array) { int[] count = new int[151]; for (int i = 0; i < array.length; i++) { count[array[i].getKey()]++; } int index = 0; for (int i = 0; i < count.length; i++) { for (int j = 0; j < count[i]; j++) { Container temp = new Container(i); array[index] = temp; index++; } } } } 

Container code :

public class Container { private int key; private String msg; public Container(int var1) { this(var1, (String)null); } public Container(int var1, String var2) { this.key = var1; this.msg = var2; } public int getKey() { return this.key; } public String getMsg() { return this.msg; } public String toString() { return "" + this.key; } } 

This is the output of the code above

Counting took 3 ms Cocktail took 760 ms Quick took 16 ms

Its needs to match the picture not exactly but somewhat

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!