Bubble Sort in Java Application

The following Java program is a Bubble Sort in Data Structure. . I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
  1. public class BubbleSort{
  2. static int myNumbers[]=new int[5];
  3. public static void main(String[] args){
  4. myNumbers[0]=6; myNumbers[1]=22;
  5. myNumbers[2]=9; myNumbers[3]=100;
  6. myNumbers[4]=45;
  7. bubbleSort(myNumbers);
  8.  
  9. for(int x =0; x< myNumbers.length; x++){
  10. System.out.println(myNumbers[x]);
  11. }
  12. }
  13. public static int[] bubbleSort(int array[]){
  14. boolean swappedOnPrevRun = true;
  15. while(swappedOnPrevRun){
  16. swappedOnPrevRun=false;
  17. for(int i = 0; i < array.length - 1; i++){
  18. if (array[i] > array[i+1]){
  19. swappedOnPrevRun = true;
  20. int temp = array[i];
  21. array[i]=array[i+1];
  22. array[i+1]=temp;
  23.  
  24. }
  25. }
  26. }
  27. return array;
  28. }
  29. }
Sample Run:
  1. 6
  2. 9
  3. 22
  4. 45
  5. 100
Program Algorithm: The statement public class BubbleSort is the name of the Java Class which is BubbleSort The Statement
  1. static int myNumbers[]=new int[5];
  2. public static void main(String[] args){
  3. myNumbers[0]=6; myNumbers[1]=22;
  4. myNumbers[2]=9; myNumbers[3]=100;
  5. myNumbers[4]=45;
the program creates the array The statement bubbleSort(myNumbers);pass the value of an array The statement
  1. while(swappedOnPrevRun){
  2. swappedOnPrevRun=false;
is the while statement that if the value is true, the value of an array will be swap.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
2 + 14 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.