Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. public class BubbleSort
  2. {
  3. public static void main(String[] args)
  4. {
  5. int[] list = new int[10];
  6. for(int i = 0; i < list.length; i++)
  7. {
  8. list[i] = (int) (Math.random() * 100) + 1;
  9. }
  10.  
  11. list = bubbleSort(list);
  12. for(int i = 0; i < list.length; i++)
  13. {
  14. if(i != list.length - 1)
  15. {
  16. System.out.print(list[i] + ", ");
  17. } else
  18. {
  19. System.out.print(list[i]);
  20. }
  21. }
  22. }
  23.  
  24. static int[] bubbleSort(int[] list)
  25. {
  26. int holder;
  27. boolean flag = true;
  28.  
  29. while(flag)
  30. {
  31. flag = false;
  32. for(int i = 0; i < list.length - 1; i++)
  33. {
  34. if(list[i] > list[i + 1])
  35. {
  36. holder = list[i];
  37. list[i] = list[i + 1];
  38. list[i + 1] = holder;
  39. flag = true;
  40. }
  41. }
  42. }
  43.  
  44. return list;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement