Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a program that sorts an array using various sorting algorithms and utilizing 4 different data types and prints the number of comparisions, number of

write a program that sorts an array using various sorting algorithms and utilizing 4 different data types and prints the number of comparisions, number of movements and the execution time.
sorting algorithms are provided, Insertion sort, selection sort, quick sort, merge sort, heap sort, radix sort. Should look like the provided GUI (The Gui is optional). Use Java please.
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
LISTING 23.1 InsertionSort.java 1 public class InsertionSort { 2 /** The method for sorting the numbers */ 3 public static void insertionSort(int[] list) { 4 for (int i = 1; i = 0 && list[k] > currentElement; k--) { 10 list[k + 1] = list[k]; 11 } 12 // Insert the current element into list[k + 1] 14 list[k + 1] = currentElement; 15 } 16 17 } ING 7.8 Selection Sort.java public class Selection Sort { /** The method for sorting the numbers */ public static void selection Sort(double[] list) { for (int i = 0; i list[j]) { currentMin = list[j]; currentMinIndex - j; } } // Swap list[i] with list[currentMinIndex] if necessary if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; } } 3} 16 LISTING 23.8 QuickSort.java public class QuickSort { 2 public static void quickSort(int[] list) { 3 quick Sort(list, 0, list. length - Di 4 } 5 6 public static void quickSort(int[] list, int first, int last) { 7 if (last > first) { 8 int pivotIndex partition(list, first, last): 9 quickSort(list, first, pivotIndex - 1): 10 quickSort(list, pivotIndex +. 1, last): 11 1 12 } 13 14 ** Partition the array list[first..last] 15 public static int partition(int[] list, int first, int last) { int pivot = list[first]: // Choose the first element as the pivot 17 int low - first + 1; // Index for forward search 18 int high - last; // Index for backward search 19 20 while (high > low) { 21 // Search forward from left 22 while (low pivot) 27 high- 28 29 11 Swap two elements in the list 30 if (high > low) { 31 int temp - list[high): 32 list[high) - Tist[low]: 33 list[low) temp: 34 35 36 37 while Chigh > first & Tist[high] >- pivot) 38 high- 39 40 I/ Swap pivot with list[high) 41 if (pivot > list (high)) { 42 list[first] - list[high): 43 list[high] - pivot; 44 return high: 45 1 46 else { 47 return first; 48 49 LISTING 23.9 Heap.java public class Heap extends comparabled private sava.util.Araylist - java.ut. ArrayListo Create a default heap public Heap W Find the main betwo children if left index ist.si) break // The tree is heap intodex - Tech Index Critiindeks if (st.getCT).compare To get right index)) maxIndex rightChild Index Create a help from an array of objects public Heap CEC objects) C for (int 101 0) { int parentIndex - (current Index - 1) / 21 1/ Swap if the current object is greater than its parent if (list.get(current Index) .compareToC Tist.get(parent Index)) > 0) E temp-list.get(currentIndex: list.setCurrentIndex, list.get(parent Index)): 15st.set(parent Index, tenp) return removedobject 70 71 7 74 75 18 19 20 21 22 23 24 25 26 27 28 29 JO 31 12 Get the bonds in the true public int getSize return Hasize break; // The treets a heap now current Index - parent Index: > 1 14 15 6 8 O D 1 2 3 * Remove the root from the heap public Eremove if Chist.size() - 0) return null EremovedObject - list.get(0); list.set(o list.getHst. 12 - 1): list.remove(list.size - D: int current Index - 03 while (current Index > void heapSort (D list // Create a Heap of integers HeapcE heap - new Heapo: 7 // Add elements to the heap 8 for (int-01 list. length: 16+) 9 heap.add(list): 10 11 // Remove elements from the heap 12 for (int 1 - list. length 1:10 --> 13 Hist[1] - heap.remove(): 14 1 15 16 A test method / 17 public static void main(String[] args) 18 Integer list (144.-5.-3.1.3, 1.-4. 0. 1. 2. 4. 5. 53): 19 heapSortist: 20 for (int 101 1) 5 // Merge sort the first half int) firsthall-new int[Hist.length Systen arraycopyclist, 0, firsthall, 0, list.length / mergeSort(first): 9 10 // Merge sort the second half int seconditalflength - list.length - list.length / int second half new int [secondhalflength 13 System arraycopyclistist length / . 14 Secondtaif. O, secondairength): 15 gesort (second: Merge that with second Into tist mergecir, secondital. Tist) 3 / Merge the sorted lists public static void merge(int listi. int[list to me) int currenti / Current Index in list! int current2 = 0; // Current Index in list2 int current) - 0 W/ Current Index in tem while (currenti

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions

Question

3. Define the roles individuals play in a group

Answered: 1 week ago