Question

In: Computer Science

In this programming question, n integers ranging from 0 to n-1 are stored randomly in A,...

In this programming question, n integers ranging from 0 to n-1 are stored randomly in A, an array of size n. In the class Sort.java, you are supposed to implement the following two sorting algorithms: Insertion-sort and Heap-sort. You have to work directly on the given starter code.

  • Download the starter code from the course web site. Read the starter code and make sure you understand how it works before attempting to modify it. In the given class Sort.java, Selection-sort is already implemented and is used to sort an array of numbers from 0 to 9.

    Initially, the array is: 0123456789

    After randomization, the array becomes: 8201954367

         SELECTION SORT...
    

    The array is now sorted: 0123456789

  • Implement Insertion-sort and Heap-sort in the Sort class.

  • ////////

  • import java.util.Random;

    public 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            a[i]=i;
           }
       }
      
       // display the elements in the array a, rowSize elements per row.
       public void displayArray(int[] a, int size){
           int rowSize=100;
           for (int i=0; i            if(i%rowSize==0){
                   System.out.println();
               }
               System.out.print(a[i]+ " ");
           }
           System.out.println();
       }
      
       // 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);
    }
       }
      
      
       // selectionSort
       public void selectionSort(int a[], int size){
           int i, j, min, minIndex;
           for (j=0; j            minIndex=j; min = a[j];
               for (i=j+1; i                if(a[i] < min ){minIndex=i; min = a[i];}
               }
               this.swap(a, j, minIndex);
           }
       }
      
    }   

  • ////

  • public class Starter{
       public static void main(String[] args) {
    final int SIZE = 10;
    int[] array = new int[SIZE];
      
    Sort s = new Sort();
      
    s.initializeArray(array, SIZE);
    System.out.print("Initially, the array is:");
    s.displayArray(array, SIZE);
      
    System.out.println();
    System.out.print("After randomization, the array becomes:");

    s.randomizeArray(array, SIZE);
    s.displayArray(array, SIZE);
    System.out.println("\nSELECTION SORT...\n");
    System.out.print("The array is now sorted:");

    s.selectionSort(array, SIZE);
    s.displayArray(array, SIZE);

      
    }
    }

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;
     
   }

   // display the elements in the array a, rowSize elements per row.
   public void displayArray(int[] a, int size){
       int rowSize=100;
       for (int i=0; i <size;i++) {         
           if(i%rowSize==0){
               System.out.println();
           }
           System.out.print(a[i]+ " ");
       }
       System.out.println();
   }

   // 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);
       }
   }
   //Insertion Sort
   public static 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;
       }
   }

   // selectionSort
   public void selectionSort(int a[], int size){
       int i, j, min, minIndex;
       for (j=0; j < size-1 ;j++) {       
           minIndex=j; min = a[j];
           for (i=j+1; i<size;i++) {            
               if(a[i] < min ){minIndex=i; min = a[i];}
           }
           this.swap(a, j, minIndex);
       }
   }
   //HeapSort
   public static void heapSort(int[] arr) {
       int size=arr.length;
       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 static void heapify(int[] arr) {
           int size=arr.length;
           for(int i=size/2-1;i>=0;i--) {
               heapi(arr,i,size-1);
           }
      
   }

   private static 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 Starter{
   public static void main(String[] args) {
final int SIZE = 10;
int[] array = new int[SIZE];

SSort s = new SSort();

s.initializeArray(array, SIZE);
System.out.print("Initially, the array is:");
s.displayArray(array, SIZE);

System.out.println();
System.out.print("After randomization, the array becomes:");

s.randomizeArray(array, SIZE);
s.displayArray(array, SIZE);
System.out.println("\nSELECTION SORT...\n");
System.out.print("The array is now sorted:");

s.selectionSort(array, SIZE);
s.displayArray(array, SIZE);

System.out.println();
System.out.print("After randomization, the array becomes:");

s.randomizeArray(array, SIZE);
s.displayArray(array, SIZE);
System.out.println("\nHEAP SORT...\n");
System.out.print("The array is now sorted:");

s.heapSort(array);
s.displayArray(array, SIZE);

}
}

//sample output


Related Solutions

An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus...
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design an algorithm in ​(Theta(log n)) to find the missing integer. Your algorithm should be given in pseudo code. For example, the array A could be {1, 2, 3, 4, 6, 7, 8, 9, 10} in which 5 is missing.
C++ on randomly generated integers stored in vectors. you will use routines from STL <algorithm> to...
C++ on randomly generated integers stored in vectors. you will use routines from STL <algorithm> to implement these algorithms. Using the following functions with their description const int DATA_SIZE = 200; const int SEARCH_SIZE = 100; const int DATA_SEED = 7; const int SEARCH_SEED = 9; void genRndNums( vector<int>& v, int seed ) { This routine generates random integers in the range [ LOW = 1, HIGH =200 ] and puts them in vector v. Initializes the random number generator...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and stores them into an array, where n is a parameter of the method. The method returns all the numbers above the average of the n random numbers. Use loops wherever possible. For example, if the 10 random numbers are 44 61 98 45 45 17 63 24 9 95 The method should returns an array which contains 61 98 63 95 In the main...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an ArrayList. Then search for the first instance of the number 3, print the position, and then remove it from the list.
1.Suppose n and k are two positive integers. Pick a uniformly random lattice path from (0,...
1.Suppose n and k are two positive integers. Pick a uniformly random lattice path from (0, 0) to (n, k). What is the probability that the first step is ‘up’?
In this program, the user will enter some permutation of the integers 0 to n-1, inclusive,...
In this program, the user will enter some permutation of the integers 0 to n-1, inclusive, (each integer in that range will appear exactly once) followed by -1 to indicate the end of the input. The user will never input more than 100 numbers. Your job is to store all the numbers in an array in the order they are input. For example if the user inputs 3 0 2 1 -1 then you should store the 3 at index...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Find the value of ∑(−1)^n/(3n+1) from n=0 to ∞
Find the value of ∑(−1)^n/(3n+1) from n=0 to ∞
The question asks to allow the user to integer ranging from 1 to 999 using JOPtionpane...
The question asks to allow the user to integer ranging from 1 to 999 using JOPtionpane in JAVA; and display those integers in words. For example, if the user enters 13 the program should display " Thirteen" and loop to ask the user to enter another integers. If the user enters 0, the program should quit. If the user enters an integer greater than 999, the program should display an error message and ask the user to enter an integer...
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n....
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n. Use convolution to find the mass function for X1 + X2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT