( Execution time for sorting ) Write a program that obtains the execution time of selection sort, merge sort, quick sort, heap sort, and radix sort for input sizes of 50,000, 100,000, 200,000,...

1 answer below »

(Execution time for sorting) Write a program that obtains the execution time of selection sort, merge sort, quick sort, heap sort, and radix sort for input sizes of 50,000, 100,000, 200,000, 250,000, and 300,000. Your program should create data randomly and print a table that looks like this (but which also includes execution times in milliseconds):



Capture.PNG


You can use the following code template to obtain the execution time:


long startTime = System.currentTimeMillis( );


/ ** Code that


performs the task */;


long endTime = System.currentTimeMillis( );


long executionTime = endTime - startTime;

Answered Same DayMar 26, 2021

Answer To: ( Execution time for sorting ) Write a program that obtains the execution time of selection sort,...

Rushendra answered on Mar 26 2021
162 Votes
Sorting_Algorithms/Sorting_Algorithms/.classpath

    
    
    
Sorting_Algorithms/Sorting_Algorithms/.project

     Sorting_Algorithms
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
Sorting_Algorithms/Sorting_Algorithms/README.md
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain
dependencies
## Dependency Management
The `JAVA DEPENDENCIES` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-pack/blob/master/release-notes/v0.9.0.md#work-with-jar-files-directly).
Sorting_Algorithms/Sorting_Algorithms/src/App.class
public synchronized class App {
public void App();
public static void main(String[]) throws Exception;
}
Sorting_Algorithms/Sorting_Algorithms/src/App.java
Sorting_Algorithms/Sorting_Algorithms/src/App.java
import com.generators.*;
import java.util.*;
public class App {
    public static void main(String[] args) throws Exception {

        long startTime=System.currentTimeMillis();
        FinalOutput finalOutput=new FinalOutput();
        String[][] finalArray=finalOutput.getOutput();
        List generatorList=new ArrayList<>();
        SortingGenerator generateMergeSort = new GenerateMergeSort();
        generatorList.add(generateMergeSort);
        SortingGenerator generateRadixSort = new GenerateRadixSort();
        generatorList.add(generateRadixSort);
        SortingGenerator generateSelectionSort = new GenerateSelectionSort();
        generatorList.add(generateSelectionSort);
        SortingGenerator generateQuickSort = new GenerateQuickSort();
        generatorList.add(generateQuickSort);
        SortingGenerator generateHeapSort = new GenerateHeapSort();
        generatorList.add(generateHeapSort);
        generatorList.forEach(gen->gen.generateSort(finalArray));
        finalOutput.printArray();
        long endTime=System.currentTimeMillis();
        long executionTime=(endTime-startTime)/1000;
        System.out.println("\nTotal time taken  : " + executionTime + " sec");
    }

}
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/HeapSort.class
package com.algorithms;
public synchronized class HeapSort {
public void HeapSort();
public void sort(int[]);
void heapSort(int[], int, int);
}
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/HeapSort.java
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/HeapSort.java
package com.algorithms;
public class HeapSort {

    public void sort(int arr[]) {

        int arrSize = arr.length;
        for (int x = arrSize/2-1; x >= 0; x--) {
            heapSort(arr, arrSize, x);
        }

        for (int y = arrSize-1; y >= 0; y--) {
            //Kind of swap
            int temp = arr[0];
            arr[0] = arr[y];
            arr[y] = temp;
            heapSort(arr, y, 0);
        }
    }

    void heapSort(int arr[], int size, int index) {
        int largestValue = index;
        int left = 2*index + 1; 
        int right = 2*index + 2;

        if (left < size && arr[left] > arr[largestValue]) {
            largestValue = left;
        }

        if (right < size && arr[right] > arr[largestValue]) {
            largestValue = right;
        }

        if (largestValue != index) {
            int swap = arr[index];
            arr[index] = arr[largestValue];
            arr[largestValue] = swap;
            heapSort(arr, size, largestValue);
        }
    }
}
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/MergeSort.class
package com.algorithms;
public synchronized class MergeSort {
public void MergeSort();
void mergeSort(int[], int, int, int);
public void arraySort(int[], int, int);
}
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/MergeSort.java
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/MergeSort.java
package com.algorithms;
public class MergeSort {

    void mergeSort(int arr[], int left, int middle, int right) {
        int sizeOne = middle - left + 1;
        int sizeTwo = right - middle;
 
        int leftElement[] = new int[sizeOne];
        int rightElement[] = new int[sizeTwo];
 
        for (int i = 0; i < sizeOne; ++i) {
            leftElement[i] = arr[left + i];
        }
        for (int j = 0; j < sizeTwo; ++j) {
            rightElement[j] = arr[middle + 1 + j];
        }
 
        int x = 0, y = 0;
        int subLeft = left;
        while (x < sizeOne && y < sizeTwo) {
            if (leftElement[x] <= rightElement[y]) {
                arr[subLeft] = leftElement[x];
                x++;
            }
            else {
                arr[subLeft] = rightElement[y];
                x++;
            }
            subLeft++;
        }
        while (x < sizeOne) {
            arr[subLeft] = leftElement[x];
            x++;
            subLeft++;
        }
 
        while (y < sizeTwo) {
            arr[subLeft] = rightElement[y];
            y++;
            subLeft++;
        }
    }
 
    public void arraySort(int array[], int left, int right) {
        if (left < right) {
            int mid =left+ (right-left)/2;
            arraySort(array, left, mid);
            arraySort(array, mid + 1, right);
            mergeSort(array, left, mid, right);
        }
    }
}
Sorting_Algorithms/Sorting_Algorithms/src/com/algorithms/QuickSort.class
package com.algorithms;
public synchronized class QuickSort {
public void QuickSort();
public int divide(int[], int, int);
public void quickSort(int[], int,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here