Advertisement
Guest User

Untitled

a guest
May 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. BUBBLE SORT
  2. public void sort(T[] array, int leftIndex, int rightIndex) {
  3. if (leftIndex >= 0 && rightIndex < array.length){
  4. for (int i = leftIndex; i < rightIndex; i++){
  5. for (int j = i+1; j <= rightIndex; j++){
  6. if (array[i].compareTo(array[j]) == 1){
  7. Util.swap(array, i, j);
  8. }
  9. }
  10. }
  11. }
  12. }
  13.  
  14. SELECTION SORT
  15. public void sort(T[] array, int leftIndex, int rightIndex) {
  16. if (leftIndex >= 0 && rightIndex < array.length){
  17. for (int i = leftIndex; i < rightIndex; i++){
  18. int min = i;
  19. for (int j = i; j <= rightIndex; j++){
  20. if (array[min].compareTo(array[j]) == 1){
  21. min = j;
  22. }
  23. }
  24. Util.swap(array, min, i);
  25. }
  26. }
  27. }
  28. INSERTION SORT
  29. public void sort(T[] array, int leftIndex, int rightIndex) {
  30. if (leftIndex >= 0 && rightIndex < array.length){
  31. for (int i = 1; i <= rightIndex; i++){
  32. int key = i;
  33. while ((key > 0) && (array[key].compareTo(array[key-1]) == -1)){
  34. Util.swap(array, key, key-1);
  35. key--;
  36. }
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement