Advertisement
MKbear

JawaW6.2

Sep 28th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class SortWithCompare{
  4.     public static class Student{
  5.         String name;
  6.         String lastname;
  7.         int grade;
  8.         public Student(String name, String lastname, int grade) {
  9.             this.name = name;
  10.             this.lastname= lastname;
  11.             this.grade= grade;
  12.         }
  13.         public int getGrade() {
  14.             return grade;
  15.         }
  16.         public String toString() {
  17.             return "Student{" +
  18.                     "name='" + name + '\'' +
  19.                     ", lastname='" + lastname+ '\'' +
  20.                     ", grade=" + grade +
  21.                     '}';
  22.         }
  23.     }
  24.  
  25.     public static class Hero implements Comparator<SortWithCompare.Student>{
  26.         public int compare(Student st1, Student st2) {
  27.             return Integer.compare(st1.getGrade(), st2.getGrade());
  28.         }
  29.         public void sortByScore(Student[] students, int low, int high) {
  30.             if (students.length == 0)
  31.                 return;
  32.             if (low >= high)
  33.                 return;
  34.             int middle = low + (high - low) / 2;
  35.             int opora = students[middle].getGrade();
  36.             int i = low, j = high;
  37.             while (i <= j) {
  38.                 while (students[i].getGrade() < opora) {
  39.                     i++;
  40.                 }
  41.                 while (students[j].getGrade() > opora) {
  42.                     j--;
  43.                 }
  44.                 if (compare(students[i], students[j]) >= 0) {
  45.                     var temp = students[i];
  46.                     students[i] = students[j];
  47.                     students[j] = temp;
  48.                     i++;
  49.                     j--;
  50.                 }
  51.             }
  52.             if (compare(students[low], students[j])>0)
  53.                 sortByScore(students, low, j);
  54.             if (compare(students[high], students[i])<0)
  55.                 sortByScore(students, i, high);
  56.         }}
  57.  
  58.     public static void main(String[] args) {
  59.         Student[] students = new Student[6];
  60.         students[0] = new Student("kirill", "Lop", 1100);
  61.         students[1] = new Student("kiri", "Lop", 536);
  62.         students[2] = new Student("kiril", "Lop", 89);
  63.         students[3] = new Student("kiri", "Lop", 10);
  64.         students[4] = new Student("kiri", "Lop", 64);
  65.         students[5] = new Student("kiri", "Lop", 96);
  66.         Hero s = new Hero();
  67.         s.sortByScore(students,0, students.length - 1);
  68.         for (Student student : students) {
  69.             System.out.println(student);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement