Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. public class BubbleSort {
  2. public static void bubbleSort(int[] arr) {
  3. boolean stillSwappingElements = true;
  4. int j = 0;
  5.  
  6. while (stillSwappingElements) {
  7. stillSwappingElements = false;
  8. j++;
  9. for (int i = 0; i < (arr.length - j); ++i) {
  10. if (arr[i] > arr[i+1]) {
  11. int temp = arr[i];
  12. arr[i] = arr[i+1];
  13. arr[i+1] = temp;
  14. stillSwappingElements = true;
  15. }
  16. }
  17. }
  18. }
  19.  
  20. public static void main(String[] args) {
  21. int[] arr = new int[] { 44, 55, 12, 42, 94, 18 };
  22.  
  23. bubbleSort(arr);
  24.  
  25. for (int element : arr) {
  26. System.out.print(element + " ");
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement