Bubble Sort in Java Application
Submitted by GeePee on Thursday, May 28, 2015 - 23:47.
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.
Sample Run:
the program creates the array
The statement is the while statement that if the value is true, the value of an array will be swap.
- public class BubbleSort{
- static int myNumbers[]=new int[5];
- myNumbers[0]=6; myNumbers[1]=22;
- myNumbers[2]=9; myNumbers[3]=100;
- myNumbers[4]=45;
- bubbleSort(myNumbers);
- for(int x =0; x< myNumbers.length; x++){
- }
- }
- public static int[] bubbleSort(int array[]){
- boolean swappedOnPrevRun = true;
- while(swappedOnPrevRun){
- swappedOnPrevRun=false;
- for(int i = 0; i < array.length - 1; i++){
- if (array[i] > array[i+1]){
- swappedOnPrevRun = true;
- int temp = array[i];
- array[i]=array[i+1];
- array[i+1]=temp;
- }
- }
- }
- return array;
- }
- }
6
9
22
45
100
Program Algorithm:
The statement public class BubbleSort
is the name of the Java Class which is BubbleSort
The Statement - static int myNumbers[]=new int[5];
- myNumbers[0]=6; myNumbers[1]=22;
- myNumbers[2]=9; myNumbers[3]=100;
- myNumbers[4]=45;
bubbleSort(myNumbers);
pass the value of an array
The statement- while(swappedOnPrevRun){
- swappedOnPrevRun=false;
Add new comment
- 87 views