a simple bubblesort example.

Bubbles.java 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import java.util.Arrays;
  2. /**
  3. * Write a description of class Bubbles here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. public class Bubbles
  9. {
  10. // instance variables - replace the example below with your own
  11. private int x;
  12. /**
  13. * Constructor for objects of class Bubbles
  14. */
  15. public Bubbles()
  16. {
  17. // initialise instance variables
  18. x = 0;
  19. }
  20. /**
  21. * An example of a method - replace this comment with your own
  22. *
  23. * @param y a sample parameter for a method
  24. * @return the sum of x and y
  25. */
  26. public int[] bubbleSort(int[] a)
  27. {
  28. System.out.println("0 : " +Arrays.toString(a));
  29. boolean flag = true;
  30. while (flag == true) {
  31. flag=false;
  32. for (int i = 0; i < a.length-1; i++) {
  33. if (a[i] > a[i+1]) {
  34. flag = true;
  35. int temp = a[i];
  36. a[i] = a[i+1];
  37. a[i+1] = temp;
  38. }
  39. }
  40. System.out.println(" : " + Arrays.toString(a));
  41. }
  42. System.out.println("end " + Arrays.toString(a));
  43. return a;
  44. }
  45. }