Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. /**
  2. * Sorts this list according to the order induced by the specified
  3. * Comparator.
  4. * */
  5. public void sort(Comparator<T> c) {
  6. //Arrays.sort(arrayList, FIRST_ELEMENT, arrayList.length);
  7. // using quick sort
  8. T obj1 = arrayList[FIRST_ELEMENT], obj2 = arrayList[FIRST_ELEMENT+1];
  9. int value;
  10. T[] temparr = (T[]) new Object[arrayList.length];
  11.  
  12. for(int i = 0; i < temparr.length; i++) {
  13. if(temparr[i] != null) {
  14. obj1 = temparr[i];
  15. }
  16. for(int j = 1; j < arrayList.length; j++) {
  17. obj2 = arrayList[j];
  18. value = c.compare(obj1, obj2);
  19. if(value > 0) {
  20. temparr[i] = obj2;
  21.  
  22. } else if(value == 0) {
  23. temparr[i] = obj1;
  24. temparr[i+1] = obj2;
  25. } else {
  26. temparr[i] = obj1;
  27. }
  28.  
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement