Question

In: Computer Science

Run Solver.java, where 20 array instances of 1M random integers are sorted (using Insertion- sort and...

  • Run Solver.java, where 20 array instances of 1M random integers are sorted (using Insertion- sort and Heap-sort, respectively). Solver.java is given, however the two sorting algorithms of course are implemented by yourself.

  • Report the time elapsed running Solver.java, using Insertion-sort, and Heap-sort, respectively. Based on your results, comment comparatively on time-efficiency of the two sorting algorithms

  • //////

  • 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<Instances; i++) {
       s.initializeArray(TwoDimArray[i], SIZE);
       s.randomizeArray(TwoDimArray[i], SIZE);
    }
      

      
    final long startTime = System.nanoTime();
    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));
      
    }
    }

Solutions

Expert Solution

//Java program

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 <size;i++) a[i]=i;
         
       }
  
       public void insertionSort(int a[],int n)
       {
           for(int i=1;i<n;i++){
               int j=i-1,val=a[i];
               while(j>=0&&a[j]<val){
               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);
           }
                  
          
       }
  
   }


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<Instances; i++) {
   s.initializeArray(TwoDimArray[i], SIZE);
   s.randomizeArray(TwoDimArray[i], SIZE);
}


final long startTime = System.nanoTime();
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));

}
}


Related Solutions

The insertion sort algorithm updates its sorted array as each new data value is entered. It...
The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step. • Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case. • Assume that we have thus far read N values and these are sorted in correct order (largest to...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random values for an array and sorted values; sorted the same list twice and collect time each time) for the following array sizes: 1000, 2000, and 10000. You should be able to confirm that the runtime is n^2 for unsorted list (i.e., going from 1000 to 2000 should be about 4 times slower and going from 1000 to 10000 should be about 100times slower). Question...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at any one time Main idea: break data into blocks, sort blocks into runs, merge runs less. Call inFile1 our source file (the one with the initial 140 records to be sorted. You will also need an inFile2, and 2 other files outFile1 and outFile2. Break the file into blocks of size 20: in this case there will be 7 blocks ( 140/20 ) Sort...
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20,...
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method given an initial invocation of binarySearch(45, 0, 15)? Answer Choices : 2     0     3     4     1
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Q1: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24,...
Q1: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method, given an initial invocation of binarySearch(25, 0, 15)? Answer Choices : 3     2     4     0     1 Q2: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60....
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT