jon waek
Crypto Compliance Analyst
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.
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
the program creates the array
The statement bubbleSort(
myNumbers)
;
pass the value of an array
The statement
is the while statement that if the value is true, the value of an array will be swap.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
- public
class
BubbleSort{
- static
int
myNumbers[
]
=
new
int
[
5
]
;
- public
static
void
main(
String
[
]
args)
{
- 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++
)
{
- System
.out
.println
(
myNumbers[
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;
- }
- }
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
- static
int
myNumbers[
]
=
new
int
[
5
]
;
- public
static
void
main(
String
[
]
args)
{
- myNumbers[
0
]
=
6
;
myNumbers[
1
]
=
22
;
- myNumbers[
2
]
=
9
;
myNumbers[
3
]
=
100
;
- myNumbers[
4
]
=
45
;
the program creates the array
The statement bubbleSort(
myNumbers)
;
pass the value of an array
The statement
- while
(
swappedOnPrevRun)
{
- swappedOnPrevRun=
false
;
is the while statement that if the value is true, the value of an array will be swap.