Question

In: Computer Science

Can someone fix solver class. it suppose to call method from sort class for insertion and...

Can someone fix solver class. it suppose to call method from sort class for insertion and heap sort. However its not giving the outuput..

Run following class Solver where 20 array instances of 1M random integers are sorted using Insertion- sort and Heap-sort, respectively

  • public class Solver {
       public static void main(String[] args) {
    final int SIZE = 1000000; // 1 million
    final int Instances=20;
    int[][] TwoDimArray = new int[Instances][SIZE];
      
    Sort s = new Sort();
      
    for(int i=0; i    s.initializeArray(TwoDimArray[i], SIZE);
       s.randomizeArray(TwoDimArray[i], SIZE);
    }
        
    final long startTime = System.nanoTime();
    for(int i=0; i s.insertionSort(TwoDimArray[i], SIZE);
    //s.heapSort(TwoDimArray[i], SIZE);
    }
      
    final long duration = (System.nanoTime() - startTime)/ 1000000;
    System.out.println("Duration in seconds:" + (duration/1000.0));
      
    }
    }

  • ///

  • import java.util.Random;

  • class Sort {
      
           // swap the ith element with the jth elements.
           private void swap(int[] a, int i, int j){
               int temp;
               temp = a[i]; a[i] = a[j]; a[j] = temp;
           }
      
           // initialize the array a with elements from 0 to size-1.
           public void initializeArray(int[] a, int size){
               for (int i=0;i          
           }
      
           public void insertionSort(int a[],int n)
           {
               for(int i=1;i                int j=i-1,val=a[i];
                   while(j>=0&&a[j]                a[j+1]=a[j];
                   j--;}
                   a[++j]=val;
               }
           }
      
           // randomly swap two elements in a for SWAPTIMES times.
           public void randomizeArray(int[] a, int size){
               final int SWAPTIMES = 10000;
               Random r = new Random();
               int j, k;
               for(int i=0; i< SWAPTIMES; i++){
                   j = r.nextInt(size);
                   k = r.nextInt(size);
                   this.swap(a, j, k);
               }
           }
      
      
          public void heapSort(int[] arr , int size) {
               heapify(arr);
               while(size>0) {
                   int temp = arr[0];
                   arr[0]=arr[size-1];
                   arr[size-1] = temp;
                   size--;
                   heapi(arr,0,size-1);
               }
              
           }

          

           private void heapify(int[] arr) {
                   int size=arr.length;
                   for(int i=size/2-1;i>=0;i--) {
                       heapi(arr,i,size-1);
                   }
              
           }

           private void heapi(int[] arr, int i,int size) {
               int left=2*i+1;
               int right=2*i+2;
               int s=i;
               if(left<=size&&arr[left]>arr[i]) {
                   s=left;
               }
               if(right<=size&&arr[right]>arr[s]) {
                   s=right;
               }
               if(i!=s) {
                   int temp=arr[i];
                   arr[i]=arr[s];
                   arr[s]=temp;
                   heapi(arr,s,size);
               }
                      
              
           }
      
       }

Solutions

Expert Solution

Sort.java
import java.util.Random;

class Sort {

    // swap the ith element with the jth elements.
    private void swap(int[] a, int i, int j) {
        int temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    // initialize the array a with elements from 0 to size-1.
//    removed code:
//    public void initializeArray(int[] a, int size){
//        for (int i=0;i
//    }
    public void initializeArray(int[] a, int size) {
        for (int i = 0; i < size; i++)
            a[i] = i;
    }

//    removed code:
//    public void insertionSort(int a[],int n)
//    {
//        for(int i=1;i                int j=i-1,val=a[i];
//        while(j>=0&&a[j]                a[j+1]=a[j];
//        j--;}
//    a[++j]=val;
//}
//       }
    public void insertionSort(int a[], int n) {
        for (int i=0; i < n; i++){
            int temp = a[i];
            int k = i-1;
            while (k >= 0 && a[k] > temp){
                a[k+1] = a[k];
                k--;
            }
            a[k+1] = temp;
        }
    }


    // randomly swap two elements in a for SWAPTIMES times.
    public void randomizeArray(int[] a, int size) {
        final int SWAPTIMES = 10000;
        Random r = new Random();
        int j, k;
        for (int i = 0; i < SWAPTIMES; i++) {
            j = r.nextInt(size);
            k = r.nextInt(size);
            this.swap(a, j, k);
        }
    }


    public void heapSort(int[] arr, int size) {
        heapify(arr);
        while (size > 0) {
            int temp = arr[0];
            arr[0] = arr[size - 1];
            arr[size - 1] = temp;
            size--;
            heapi(arr, 0, size - 1);
        }

    }


    private void heapify(int[] arr) {
        int size = arr.length;
        for (int i = size / 2 - 1; i >= 0; i--) {
            heapi(arr, i, size - 1);
        }

    }

    private void heapi(int[] arr, int i, int size) {
        int left = 2 * i + 1;
        int right = 2 * i + 2;
        int s = i;
        if (left <= size && arr[left] > arr[i]) {
            s = left;
        }
        if (right <= size && arr[right] > arr[s]) {
            s = right;
        }
        if (i != s) {
            int temp = arr[i];
            arr[i] = arr[s];
            arr[s] = temp;
            heapi(arr, s, size);
        }
    }
}

Solver.java:

import java.text.SimpleDateFormat;

public class Solver {
    public static void main(String[] args) {
        final int SIZE = 1000000; // 1 million
        final int Instances = 20;
        int[][] TwoDimArray = new int[Instances][SIZE];

        Sort s = new Sort();

//        removed code:
//        for(int i=0; i    s.initializeArray(TwoDimArray[i], SIZE);
//        s.randomizeArray(TwoDimArray[i], SIZE);
//    }
        for (int i = 0; i < Instances; i++) {
            s.initializeArray(TwoDimArray[i], SIZE);
        }


        final long startTime = System.nanoTime();

//        removed code:
//        final long startTime = System.nanoTime();
//        for(int i=0; i s.insertionSort(TwoDimArray[i], SIZE);
////s.heapSort(TwoDimArray[i], SIZE);
//    }
        for (int i = 0; i < Instances; i++) {
            s.insertionSort(TwoDimArray[i], SIZE);
//            s.heapSort(TwoDimArray[i], SIZE);
        }


        final long duration = (System.nanoTime() - startTime) / 1000000;
        System.out.println("Duration in seconds:" + (duration / 1000.0));
    }
}

Output:


Related Solutions

Can someone implement the following in Java? Quicksort with switching to Insertion sort when the number...
Can someone implement the following in Java? Quicksort with switching to Insertion sort when the number of elements in the subarray is less than or equal to 2% of the original number Requirements: 1) functions from standard libraries implementing Quicksort are NOT allowed;
Can someone fix this code so that it can count comparison and exchange? import java.util.Arrays; class...
Can someone fix this code so that it can count comparison and exchange? import java.util.Arrays; class Main { static int comparisons; static int exchanges; // Recursive function to perform insertion sort on sub-array arr[i..n] public static void insertionSort(int[] arr, int i, int n) { int value = arr[i]; int j = i; // Find index j within the sorted subset arr[0..i-1] // where element arr[i] belongs while (j > 0 && arr[j - 1] > value) { arr[j] = arr[j...
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
For an assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by...
For an assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by their depth from largest depth to smallest depth using the selection sort algorithm. Modify this method to do exactly 50 passes and then modify testSort to run this method on the file earthQuakeDataDec6sample2.atom. The file may not be completely sorted as there are more than 50 quakes in the file. After running your program of 50 Selection sort passes on this file, what is...
**Please show all work and explain** Very confused We can express insertion sort as a recursive...
**Please show all work and explain** Very confused We can express insertion sort as a recursive procedure as follows. In order to sort A[1..n], we recursively sort A[1..n-1] and then insert A[n] into the sorted array A[1..n-1]. Write a recurrence for the running time of this recursive version of insertion sort.
I want to know how can I fix this program so it can work properly....can someone...
I want to know how can I fix this program so it can work properly....can someone run it and give me the fixed version but do not the entire program bear with this one please and thanks in advance!!!!! import java.util.Scanner; public class ifPractice { public static void main(String[] args) { Scanner KB = new Scanner(System.in); { double option; double option02; double stu_gradeTest01; double stu_gradeTest02; double stu_gradeTest03; double final_grade; { System.out.println("Enter #1 to see what test to grade first, #2...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT