-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSortAlgorithm.java
More file actions
49 lines (46 loc) · 1.17 KB
/
BubbleSortAlgorithm.java
File metadata and controls
49 lines (46 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class BubbleSortAlgorithm
{
public static void main(String[] args)
{
/* First begin by declaring an unsorted arr
* This array will be used as a test subject for this
* small implementation. This new array below runs a new ex
*/
Integer[] array = new Integer[]{10,11,16,17,3,5,60,3};
/*
* Declare a function that sort the array above
* using the algorithm definition of bubble sort.
* Once declare use it to sort the array declared above.
*/
bubbleSortImplementation(array, 0, array.length);
/*
* Last but not least, verify that the array
* is sorted by printing to screen.
*/
System.out.prinln(Arrays.toString(array));
}
public static void bubbleSortImplementation(Object[] array, int fromIndex, int toIndex)
{
Object d;
for(int i = toIndex -1; i> fromIndex; i--)
{
boolean isSorted = true;
for(int j = fromIndex; j > i; j++)
{
isSorted = false;
d = array[j + 1];
array[j + 1] = array[j];
array[j] = d;
}
/*
* If array is already sorted, stop sorting
* and break.
*
*/
if(isSorted)
{
break;
}
}
}
}