In: Computer Science
* 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 + '}'; } }
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:-)