Guest User

Untitled

a guest
May 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. Grade guilherme = new Grade("guilherme", 7);
  4.  
  5. Grade[] grades = {
  6. new Grade("Andre", 4),
  7. new Grade("Carlos", 8.5),
  8. new Grade("Ana", 10),
  9. new Grade("Jonas", 3),
  10. new Grade("Juliana", 6.7),
  11. new Grade("Lucia", 9.3),
  12. new Grade("Paulo", 9),
  13. new Grade("Mariana", 5),
  14. guilherme
  15. };
  16.  
  17. sortByQuickSort(grades, 0, grades.length);
  18.  
  19. for (Grade grade : grades) {
  20. System.out.println(grade.getStudent() + " grade: " + grade.getValue());
  21. }
  22. }
  23.  
  24. private static void sortByQuickSort(Grade[] grades, int start, int end) {
  25. int elements = end - start;
  26.  
  27. if (elements > 1) {
  28. int pivot = split(grades, start, end);
  29. sortByQuickSort(grades, start, pivot);
  30. sortByQuickSort(grades, pivot +1, end);
  31. }
  32.  
  33. }
  34.  
  35. private static int split(Grade[] grades, int start, int end) {
  36. Grade pivot = grades[end -1];
  37. int lesserThanPivot = 0;
  38.  
  39. for (int checking = 0; checking < end -1; checking++) {
  40. Grade checkingGrade = grades[checking];
  41.  
  42. if (checkingGrade.getValue() <= pivot.getValue()) {
  43. change(grades, checking, lesserThanPivot);
  44. lesserThanPivot++;
  45. }
  46. }
  47.  
  48. change(grades, end -1, lesserThanPivot);
  49.  
  50. return lesserThanPivot;
  51. }
  52.  
  53. private static void change(Grade[] grades, int index1, int index2) {
  54. Grade grade1 = grades[index1];
  55. Grade grade2 = grades[index2];
  56.  
  57. grades[index1] = grade2;
  58. grades[index2] = grade1;
  59. }
Add Comment
Please, Sign In to add comment