Question

In: Computer Science

* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...

* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place.

*

* @param students array to be sorted, can be empty, but cannot be null

*/

public static void sortGPA(Student[] students)

{

// TODO: implement this

}

Student class:

public class Student extends Person {

  private double GPA;

  public Student(String lastName, String firstName, double gpa) {
    super(lastName, firstName);
    this.GPA = gpa;
  }

  public double getGPA() {
    return GPA;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }
    Student student = (Student) o;
    return Double.compare(student.GPA, GPA) == 0;
  }

  @Override
  public String toString() {
    return "Student{" + super.toString() + ";" +
        "GPA=" + GPA +
        '}';
  }
}

Solutions

Expert Solution

Implementation in JAVA:


import java.util.Scanner;

public class Student_sort {

//   statically defined Scanner, Student array,and size
   static Scanner s= new Scanner(System.in);
   static Student arr[]= new Student[10];
   static int size=0;
  
//   driver method or main method
   public static void main(String[] args) {
      
       System.out.println("Insertion : ");
       insert();
       System.out.println();
      
       insert();
       System.out.println();
      
       insert();
       System.out.println();
      
       insert();
       System.out.println();
      
       insert();
       System.out.println();
      
       System.out.println("Size of array is: "+size());
         
       System.out.println();
      
       System.out.println("Print without sorting : ");
      
       printlist(arr);
      
      
      
       sortGPA(arr);
         
      
         
   System.out.println();
       System.out.println("Print After sorting : ");
         
         
       printlist(arr);
      
      

   }
  
//   return size
   public static int size() {
       return size;
   }
  
//   Insert data of Student in Array
   public static void insert() {
      
       System.out.print("Enter Student's Firstname : ");
       String fname=s.next();
       s.nextLine();
      
       System.out.print("Enter Student's Lastname : ");
       String lname=s.next();
       s.nextLine();
      
       System.out.print("Enter Student's GPA : ");
       double gpa=s.nextDouble();
      
       Student st= new Student(fname,lname,gpa);
      
       arr[size]=st;
      
       size++;
      
      
   }
  
//   print all data of Student array
   public static void printlist(Student arr[]) {
      
       System.out.println("FirstName LastName GPA ");
       System.out.println(" ");
      
       for(int i=0;i<size;i++) {
         
           Student st= arr[i];
             
   System.out.println(st.firstName+" "+ " "+st.lastName+" "+ " "+st.getGPA());
  
       }
      
   }

//   method for sorting Student array in descending order
//   in according to GPA
   public static void sortGPA(Student[] students)

   {
       double[] gpa= new double[size];
      

       for(int i=0;i<size;i++) {
         
           Student st= students[i];
           gpa[i]=st.getGPA();
             
             
       }

// apply merge sort
merge_sort(gpa,0,size-1);

// reverse array to produce descending order
double k, t;
for (int i = 0; i < size / 2; i++) {
t = gpa[i];
gpa[i] = gpa[size - i - 1];
gpa[size - i - 1] = t;
}



// sort descending Student array in order of GPA
Student[] temp= new Student[size];

for(int i=0;i<size;i++) {
     
   double gp=gpa[i];
     
   for(int j=0;j<size;j++) {
         
       Student st= students[j];
       if(st.getGPA()==gp) {
           temp[i]=st;
           break;
       }
   }
     
   }

for(int i=0;i<size;i++) {
     
   students[i]=temp[i];
  
}

      

   }
  
//   merge sort array
   static void merge_sort(double arr[], int l, int r)
   {
   if (l < r) {
   // Find the middle point
   int m = (l + r) / 2;
  
   // Sort first and second halves


   merge_sort(arr, l, m);
   merge_sort(arr, m + 1, r);
  
   // Merge the sorted halves
   merge(arr, l, m, r);
   }
   }
  
//   method of merge 2 array
   static void merge(double arr[], int l, int m, int r)
   {
   // Find sizes of two subarrays to be merged
   int n1 = m - l + 1;
   int n2 = r - m;
  
   /* Create temp arrays */
   double L[] = new double[n1];
   double R[] = new double[n2];
  
   /*Copy data to temp arrays*/
   for (int i = 0; i < n1; ++i)
   L[i] = arr[l + i];
   for (int j = 0; j < n2; ++j)
   R[j] = arr[m + 1 + j];
  
   /* Merge the temp arrays */
  
   // Initial indexes of first and second subarrays
   int i = 0, j = 0;
  
   // Initial index of merged subarry array
   int k = l;
   while (i < n1 && j < n2) {
   if (L[i] <= R[j]) {
   arr[k] = L[i];
   i++;
   }
   else {
   arr[k] = R[j];
   j++;
   }
   k++;
   }
  
   /* Copy remaining elements of L[] if any */
   while (i < n1) {
   arr[k] = L[i];
   i++;
   k++;
   }
  
   /* Copy remaining elements of R[] if any */
   while (j < n2) {
   arr[k] = R[j];
   j++;
   k++;
   }
   }
  
     
  
}

// Student class
class Student {

   private double GPA;
   String lastName;
   String firstName;

   public Student( String firstName,String lastName, double gpa) {
  
       this.lastName=lastName;
       this.firstName=firstName;
   this.GPA = gpa;
   }

   public double getGPA() {
   return GPA;
   }

   @Override
   public boolean equals(Object o) {
   if (this == o) {
   return true;
   }
   if (o == null || getClass() != o.getClass()) {
   return false;
   }
   if (!super.equals(o)) {
   return false;
   }
   Student student = (Student) o;
   return Double.compare(student.GPA, GPA) == 0;
   }

   @Override
   public String toString() {
   return "Student{" + super.toString() + ";" +
   "GPA=" + GPA +
   '}';
   }
   }

SAMPLE OUTPUT:

If you have any doubt regarding this question please ask me in comments

THANK YOU:-)


Related Solutions

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.
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
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) {...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT