Advertisement
malixds_

21/22

Jan 16th, 2023
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. public interface Algorithm {
  2.     void sort(Comparable[] list);
  3. }
  4.  
  5. // A class for bubble sort
  6. public static class BubbleSort implements Algorithm {
  7.     @Override
  8.     public void sort(Comparable[] list) {
  9.         int n = list.length;
  10.         for (int i = 0; i < n - 1; i++) {
  11.             for (int j = 0; j < n - i - 1; j++) {
  12.                 if (list[j].compareTo(list[j + 1]) > 0) {
  13.                     Comparable temp = list[j];
  14.                     list[j] = list[j + 1];
  15.                     list[j + 1] = temp;
  16.                 }
  17.             }
  18.         }
  19.     }
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement