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...
**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.
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...
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists....
USE JAVA. 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...
Code: C++ Write a very general sort method that can sort any type of data arrays/lists....
Code: C++ 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT