Question
Here is my program, what I need it to do is to generate the corresponding array of objects, prompt for the sort field , perform
Here is my program, what I need it to do is to
generate the corresponding array of objects, prompt for the sort field , perform the sorts, and show the run times for each algorithm (visible on the same screen). Also provide an option to show the (aligned) unsorted and/or sorted versions of the array contents at the user's discretion after each individual sort completes. Let the user perform the task as many times as desired without exiting the program. In addition to the standard documentation and submission requirements, submit a table that summarizes the results of running your algorithm on various values of N along with your observations about the actual relative performance of your algorithms.
import java.util.Random
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j { // If current element is smaller than or // equal to pivot if (arr[j] <= pivot) { i++; // swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // swap arr[i+1] and arr[high] (or pivot) int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; return i+1; } /* The main function that implements QuickSort() * arr[] --> Array to be sorted, * low --> Starting index, * high --> Ending index */ void quickSort(int arr[], int low, int high) { if (low < high) { /* pi is partitioning index, arr[pi] is now at right place */ int pi = partition(arr, low, high); // Recursively sort elements before // partition and after partition quickSort(arr, low, pi-1); quickSort(arr, pi+1, high); } } // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..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 L[i] = arr[l + i]; for (int j=0; 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++; } } // Main function that sorts arr[l..r] using // merge() 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 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 selectionSort(int arr[]) { int n = arr.length; // One by one move boundary of unsorted subarray for (int i = 0; i < n-1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first // element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } void insertionSort(int arr[]) { int n = arr.length; for (int i=1; i { int key = arr[i]; int j = i-1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j>=0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; } } /* function to sort arr using shellSort */ int shellSort(int arr[]) { int n = arr.length; // Start with a big gap, then reduce the gap for (int gap = n/2; gap > 0; gap /= 2) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already // in gapped order keep adding one more element // until the entire array is gap sorted for (int i = gap; i < n; i += 1) { // add a[i] to the elements that have been gap // sorted save a[i] in temp and make a hole at // position i int temp = arr[i]; // shift earlier gap-sorted elements up until // the correct location for a[i] is found int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; // put temp (the original a[i]) in its correct // location arr[j] = temp; } } return 0; } /* A utility function to print array of size n */ static void printArray(int arr[]) { int n = arr.length; for (int i=0; i System.out.print(arr[i]+" "); System.out.println(); } public static void main(String args[]) { Random rand = new Random(); int size = 10000; int arr[] = new int[size]; int temp[] = arr; // place random elements into arr for(int i=0; i arr[i] = rand.nextInt(100); long startTime, endTime; SortComparisons o = new SortComparisons(); // Performance of Bubble Sort startTime = System.currentTimeMillis(); o.bubbleSort(arr); endTime = System.currentTimeMillis(); System.out.println("Bubble Sort took " + (endTime - startTime) + " milliseconds "); // Performance of Insertions Sort arr = temp; startTime = System.currentTimeMillis(); o.insertionSort(arr); endTime = System.currentTimeMillis(); System.out.println("Insertion Sort took " + (endTime - startTime) + " milliseconds "); // Performance of Selection Sort arr = temp; startTime = System.currentTimeMillis(); o.selectionSort(arr); endTime = System.currentTimeMillis(); System.out.println("Selection Sort took " + (endTime - startTime) + " milliseconds "); // Performance of Shell Sort arr = temp; startTime = System.currentTimeMillis(); o.shellSort(arr); endTime = System.currentTimeMillis(); System.out.println("Shell sort took " + (endTime - startTime) + " milliseconds "); // Performance of Quick Sort arr = temp; startTime = System.currentTimeMillis(); o.quickSort(arr, 0, size-1); endTime = System.currentTimeMillis(); System.out.println("Quick Sort took " + (endTime - startTime) + " milliseconds "); // Performance of Merge Sort arr = temp; startTime = System.currentTimeMillis(); o.mergeSort(arr, 0, size-1); endTime = System.currentTimeMillis(); System.out.println("Merge Sort took " + (endTime - startTime) + " milliseconds "); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started