Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SortWithCompare{
- public static class Student{
- String name;
- String lastname;
- int grade;
- public Student(String name, String lastname, int grade) {
- this.name = name;
- this.lastname= lastname;
- this.grade= grade;
- }
- public int getGrade() {
- return grade;
- }
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", lastname='" + lastname+ '\'' +
- ", grade=" + grade +
- '}';
- }
- }
- public static class Hero implements Comparator<SortWithCompare.Student>{
- public int compare(Student st1, Student st2) {
- return Integer.compare(st1.getGrade(), st2.getGrade());
- }
- public void sortByScore(Student[] students, int low, int high) {
- if (students.length == 0)
- return;
- if (low >= high)
- return;
- int middle = low + (high - low) / 2;
- int opora = students[middle].getGrade();
- int i = low, j = high;
- while (i <= j) {
- while (students[i].getGrade() < opora) {
- i++;
- }
- while (students[j].getGrade() > opora) {
- j--;
- }
- if (compare(students[i], students[j]) >= 0) {
- var temp = students[i];
- students[i] = students[j];
- students[j] = temp;
- i++;
- j--;
- }
- }
- if (compare(students[low], students[j])>0)
- sortByScore(students, low, j);
- if (compare(students[high], students[i])<0)
- sortByScore(students, i, high);
- }}
- public static void main(String[] args) {
- Student[] students = new Student[6];
- students[0] = new Student("kirill", "Lop", 1100);
- students[1] = new Student("kiri", "Lop", 536);
- students[2] = new Student("kiril", "Lop", 89);
- students[3] = new Student("kiri", "Lop", 10);
- students[4] = new Student("kiri", "Lop", 64);
- students[5] = new Student("kiri", "Lop", 96);
- Hero s = new Hero();
- s.sortByScore(students,0, students.length - 1);
- for (Student student : students) {
- System.out.println(student);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement