Question: Investigating Sorting Algorithms In this assignment we will create a sorting program that provides the user with a large assortment of sorting methods and options.
Investigating Sorting Algorithms In this assignment we will create a sorting program that provides the user with a large assortment of sorting methods and options. The user should be able to choose from a menu to select which Sort they would like to use. For each Sort, be sure to sort four arrays (*see note below), and list the time it took to sort each of the four arrays either in milliseconds or nanoseconds. For each sorting algorithm, write a brief description of how the sort works, and be sure to note the time complexity. You can include both in the code as comments. The user should be able to choose from the following sort algorithms (Be sure to implement all). If you use any other resource (including online) you need to cite the source in your code
. Bogo Sort **
Selection Sort
Insertion Sort
Bubble Sort
Quick Sort
Shell Sort
Merge Sort
Gnome Sort
Cocktail Sort
Radix Sort
** One more Sort of your choice**
*Note: For the assignment, you will need to create several arrays of integers of the following sizes {20,100, 500, 1000}. Fill the values of these arrays with random values between the number 0 and 999. You may need to reinitialize these arrays if the user decides to run another sort. You may also need values of 1000 to 9999 if you want to use Radix from the in-class example. **For Bogo Sort, due to the speed you may want to give the user a choice of the array size so that it may finish the sort in a somewhat timely manner. i have following codes for sort methods so far:
import java.util.*; // for Random
public class Sorting { private static final Random RAND = new Random(42); public static void main(String[] args) { int LENGTH = 1000; int RUNS = 17; for (int i = 0; i < RUNS; i++) { int[] a = createRandomArray(LENGTH); long startTime1 = System.currentTimeMillis(); quickSort(a); long endTime1 = System.currentTimeMillis(); if (!isSorted(a)) { throw new RuntimeException("not sorted afterward: " + Arrays.toString(a)); } System.out.printf("%10d elements => %6d ms ", LENGTH, endTime1 - startTime1); LENGTH *= 2; } } public static void quickSort(int[] a) { quickSort(a, 0, a.length - 1); } private static void quickSort(int[] a, int min, int max) { if (min >= max) { return; } int pivot = a[min]; swap(a, min, max); int i = min; int j = max - 1; while (i <= j) { while (i <= j && a[i] < pivot) { i++; } while (i <= j && a[j] > pivot) { j--; } if (i <= j) { swap(a, i, j); i++; j--; } } swap(a, max, i); quickSort(a, min, i - 1); quickSort(a, i + 1, max); } public static void mergeSort(int[] a) { if (a.length >= 2) { int[] left = Arrays.copyOfRange(a, 0, a.length / 2); int[] right = Arrays.copyOfRange(a, a.length / 2, a.length); mergeSort(left); mergeSort(right); int i1 = 0; int i2 = 0; for (int i = 0; i < a.length; i++) { if (i2 >= right.length ||(i1 < left.length && left[i1] < right[i2])) { a[i] = left[i1]; i1++; } else { a[i] = right[i2]; i2++; } } } } public static void shellSort(int[] a) { for (int gap = a.length / 2; gap >= 1; gap = gap / 2) { for (int i = gap; i < a.length; i++) { int temp = a[i]; int j = i; while (j >= gap && a[j - gap] > temp) { a[j] = a[j - gap]; j -= gap; } a[j] = temp; } } } public static void insertionSort(int[] a) { for (int i = 1; i < a.length; i++) { int temp = a[i]; int j = i; while (j >= 1 && a[j - 1] > temp) { a[j] = a[j - 1]; j--; } a[j] = temp; } } public static void selectionSort(int[] a) { for (int pass = 0; pass < a.length; pass++) { int min = pass; for (int j = pass + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } swap(a, pass, min); } } public static void bubbleSort(int[] a) { for (int pass = 0; pass < a.length; pass++) { boolean changed = false; for (int i = 0; i < a.length - 1 - pass; i++) { if (a[i] > a[i + 1]) { swap(a, i, i + 1); changed = true; } } if (!changed) { return; } } } public static void bogoSort(int[] a) { while (!isSorted(a)) { shuffle(a); } } public static void heapSort(int[] a) { for (int i = a.length / 2; i >= 0; i--) { bubbleDown(a, i, a.length - 1); } for (int i = a.length - 1; i > 0; i--) { swap(a, 0, i); bubbleDown(a, 0, i - 1); } } private static void bubbleDown(int[] a, int hole, int max) { int temp = a[hole]; while (hole * 2 <= max) { int child = hole * 2; if (child != max && a[child + 1] > a[child]) { child++; } if (a[child] > temp) { a[hole] = a[child]; } else { break; } hole = child; } a[hole] = temp; } public static void bucketSort(int[] a) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int k : a) { max = Math.max(max, k); min = Math.min(min, k); } int[] counters = new int[max - min + 1]; for (int k : a) { counters[k - min]++; } int i = 0; for (int j = 0; j < counters.length; j++) { for (int k = 0; k < counters[j]; k++) { a[i] = j + min; i++; } } public static void radixSort(int[] a) { Queue
so now all i need is to write a program to give the user an option when the program starts where they can pick what sort they want to run. Like with a menu of options, and from there they can choose what sort they want to run.Can anyone please help me with this ?And this is java language.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
