• We just launched and are currently in beta. Join us as we build and grow the community.

Bubble Sort in Java Application

jon waek

Crypto Compliance Analyst
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
127
Likes
60
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. for

    (

    int

    x =

    0

    ;

    x<

    myNumbers.length

    ;

    x++

    )

    {
  9. System

    .out

    .println

    (

    myNumbers[

    x]

    )

    ;
  10. }
  11. }
  12. public

    static

    int

    [

    ]

    bubbleSort(

    int

    array[

    ]

    )

    {
  13. boolean

    swappedOnPrevRun =

    true

    ;
  14. while

    (

    swappedOnPrevRun)

    {
  15. swappedOnPrevRun=

    false

    ;
  16. for

    (

    int

    i =

    0

    ;

    i <

    array.length

    -

    1

    ;

    i++

    )

    {
  17. if

    (

    array[

    i]

    >

    array[

    i+

    1

    ]

    )

    {
  18. swappedOnPrevRun =

    true

    ;
  19. int

    temp =

    array[

    i]

    ;
  20. array[

    i]

    =

    array[

    i+

    1

    ]

    ;
  21. array[

    i+

    1

    ]

    =

    temp;

  22. }
  23. }
  24. }
  25. return

    array;
  26. }
  27. }

Sample Run:

6
9
22
45
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.

 

452,496

337,656

337,664

Top