Guest User

Untitled

a guest
Feb 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. package BubbleSort;
  2.  
  3. public class BubbleSort {
  4. public static void sort(int[] n) {
  5. while (!sorted(n)) {
  6. for (int i = 0; i < n.length - 1; i++) {
  7. if (n[i] > n[i + 1])
  8. interchange(i, i + 1, n);
  9. }
  10. }
  11. }
  12.  
  13. private static boolean sorted(int[] n) {
  14. for (int i = 0; i < n.length - 1; i ++)
  15. if(n[i] > n[i + 1]) return false;
  16. return true;
  17. }
  18.  
  19. private static void interchange(int i , int j, int[] n) {
  20. int temp = n[i];
  21. n[i] = n[j];
  22. n[j] = temp;
  23. }
  24. }
Add Comment
Please, Sign In to add comment