Now implement similar methods to test special cases for sorting. You can use my compare method as a guide. Feel free to comment out the original compare method when you are running these methods. Add these results to your table. public static void compareSorted (int n) This method creates arrays of integers 1 to n in sorted order and runs the different sorting algorithms on the arrays. public static void compareReverse Sorted (int n) This method creates an array of integers 1 to n in reverse sorted order and runs the different sorting algorithms on the arrays. public static void compare Duplicates (int n) This method creates an array of n 1s and runs the different sorting algorithms on the arrays.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

package sorting;

 

import java.util.Arrays;

import java.util.Random;

import sorting.Heap;

 

public class ComparisonSorter {

 

public static void insertionSort(int[] arr) {

for(int i=1;i<arr.length;i++) {

int j = i-1;

int toPlace = arr[i];

while(j>=0&&arr[j]>toPlace) {

arr[j+1] = arr[j];

j--;

}

arr[j+1] = toPlace;

}

}

 

public static void mergeSort(int [] arr) {

mergeSortRecurse(arr,0,arr.length-1);

}

 

private static void mergeSortRecurse(int[] arr, int start, int end) {

if(start>=end)

return;

 

int mid = start + ((end-start)/2);

mergeSortRecurse(arr,start,mid);

mergeSortRecurse(arr,mid+1,end);

merge(arr,start,mid,end);

}

 

private static void merge(int[] arr, int start, int mid, int end) {

int leftSize = mid - start +1;

int rightSize = end - mid;

 

int[] left = new int[leftSize+1];

int[] right = new int[rightSize+1];

 

int leftIndex;

int rightIndex;

 

for(leftIndex = 0; leftIndex<leftSize;leftIndex++)

left[leftIndex] = arr[start+leftIndex];

for(rightIndex = 0; rightIndex<rightSize;rightIndex++)

right[rightIndex] = arr[mid+rightIndex+1];

 

left[leftIndex] = Integer.MAX_VALUE;

right[rightIndex] = Integer.MAX_VALUE;

 

leftIndex = 0;

rightIndex = 0;

 

for(int mergeIndex = start; mergeIndex<=end;mergeIndex++) {

if(left[leftIndex] <= right[rightIndex]) {

arr[mergeIndex] = left[leftIndex];

leftIndex++;

}

else {

arr[mergeIndex] = right[rightIndex];

rightIndex++;

}

}

 

}

 

 

public static void heapSort(int[] arr)

{

// create a new instance of Heap using the given array

Heap heap = new Heap(arr, arr.length);

 

// build a max heap from the array

heap.buildMaxHeap();

 

// repeatedly extract the maximum element from the heap and place it at the end of the array

for (int i = arr.length - 1; i >= 1; i--) {

// swap the maximum element with the last element of the heap

int temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

 

// reduce the heap size to exclude the last element

heap.setHeapSize(heap.getHeapSize() - 1);

 

// restore the max heap property

heap.maxHeapify(0);

}

}

 

 

public static int partitionLomuto(int[] arr, int low, int high) {

int pivot = arr[high]; // Choose the last element as the pivot

int i = low - 1; // Initialize the index of smaller element

 

for (int j = low; j <= high - 1; j++) {

// If current element is smaller than or equal to pivot

if (arr[j] <= pivot) {

i++; // increment index of smaller element

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp; // Swap arr[i] and arr[j]

}

}

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp; // Swap arr[i + 1] and arr[high], which is the pivot

return i + 1; // Return the partition index

}

 

public static void quickSortLomuto(int[] arr, int low, int high) {

if (low < high) {

int pivotIndex = partitionLomuto(arr, low, high);

quickSortLomuto(arr, low, pivotIndex - 1); // Sort the left subarray

quickSortLomuto(arr, pivotIndex + 1, high); // Sort the right subarray

}

}

 

public static void quickSort(int[] arr)

{

 

quickSortLomuto(arr, 0, arr.length - 1);

}

 

 

public static void compare(int n) {

int[] insertionData = new int[n];

int[] mergeData = new int[n];

int[] heapData = new int[n];

int[] quickData = new int[n];

long start;

long end;

long runtime;

Random randomGenerator = new Random();

 

for(int i=0;i<n;i++) {

int j = randomGenerator.nextInt()%10001;

insertionData[i] = mergeData[i] = heapData[i] = quickData[i] = j;

}

start = System.nanoTime();

 

insertionSort(insertionData);

 

end = System.nanoTime();

runtime = end - start;

System.out.println("Insertion Sort took: " + runtime);

 

start = System.nanoTime();

 

mergeSort(mergeData);

 

end = System.nanoTime();

runtime = end - start;

System.out.println("Merge Sort took: " + runtime);

 

start = System.nanoTime();

 

heapSort(heapData);

 

end = System.nanoTime();

runtime = end - start;

System.out.println("Heap Sort took: " + runtime);

 

start = System.nanoTime();

 

quickSort(quickData);

 

end = System.nanoTime();

runtime = end - start;

System.out.println("QuickSort took: " + runtime);

 

 

 

}

 

public static void main(String[] args) {

 

compare(Integer.parseInt(args[0]));

}

 

}

Can you please help Written Prediction and Testing above the code?

Predict the order in which the algorithms will perform in practice and explain your
reasoning. You can predict that some algorithms will tie. Your reasoning should include the
runtime of each algorithm.
Create a table that compares the runtimes of the different sorting algorithms on
randomized inputs of sizes starting at 10 and increasing by a power of 10 as high as your
computer can go. You don't need to run your code forever, but expect some of the larger
sized inputs to take several minutes. For example it took my machines about 4 minutes to
run the timing method on all four sorting algorithms with input size of 1,000,000.
Now implement similar methods to test special cases for sorting. You can use my compare
method as a guide. Feel free to comment out the original compare method when you are
running these methods. Add these results to your table.
public static void compare Sorted (int n)
This method creates arrays of integers 1 to n in sorted order and runs the different sorting
algorithms on the arrays.
public static void compare Reverse Sorted (int n)
This method creates an array of integers 1 to n in reverse sorted order and runs the
different sorting algorithms on the arrays.
public static void compare Duplicates (int n)
This method creates an array of n 1s and runs the different sorting algorithms on the
arrays.
Transcribed Image Text:Predict the order in which the algorithms will perform in practice and explain your reasoning. You can predict that some algorithms will tie. Your reasoning should include the runtime of each algorithm. Create a table that compares the runtimes of the different sorting algorithms on randomized inputs of sizes starting at 10 and increasing by a power of 10 as high as your computer can go. You don't need to run your code forever, but expect some of the larger sized inputs to take several minutes. For example it took my machines about 4 minutes to run the timing method on all four sorting algorithms with input size of 1,000,000. Now implement similar methods to test special cases for sorting. You can use my compare method as a guide. Feel free to comment out the original compare method when you are running these methods. Add these results to your table. public static void compare Sorted (int n) This method creates arrays of integers 1 to n in sorted order and runs the different sorting algorithms on the arrays. public static void compare Reverse Sorted (int n) This method creates an array of integers 1 to n in reverse sorted order and runs the different sorting algorithms on the arrays. public static void compare Duplicates (int n) This method creates an array of n 1s and runs the different sorting algorithms on the arrays.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 8 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education