Advertisement
Guest User

sorte

a guest
May 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. public static <E extends Comparable<E>> Collection<E> sortList (Collection<E> list) {
  2.  
  3. if (list.isEmpty())
  4. return null;
  5.  
  6. if (list.size() == 1)
  7. return list;
  8.  
  9. Iterator<E> itr = (Itr<E>) list.iterator();
  10. ArrayList<E> arrayList = new ArrayList<>();
  11. E temp;
  12.  
  13. while(itr.hasNext()) {
  14. arrayList.add(itr.next());
  15. }
  16.  
  17. list.clear();
  18.  
  19. // Bubble Sort
  20.  
  21. for (int i = 0; i < arrayList.size() - 1; i++)
  22. for (int j = 0; j < arrayList.size() - 1 - i; j++) {
  23. if (arrayList.get(j).compareTo(arrayList.get(j+1)) > 0) {
  24. temp = arrayList.get(j);
  25. arrayList.set(j, arrayList.get(j+1));
  26. arrayList.set(j+1, temp);
  27. }
  28. }
  29.  
  30. list.addAll(arrayList);
  31.  
  32. return list;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement