-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
45 lines (36 loc) · 741 Bytes
/
BubbleSort.java
File metadata and controls
45 lines (36 loc) · 741 Bytes
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
/*
* Brett Waugh
* 3 December 2019
* BubbleSort.java
* Bubble Sort algorithm.
* Bubble Sort is known for its simplicity
* but often performs poorly.
*
*/
public class BubbleSort {
/*
* Bubble Sort logic.
*/
public static int[] sort(int[] data) {
int temp = 0;
for (int i = 0; i < data.length - 1; i++) {
for (int j = 0; j < data.length - i - 1; j++) {
if (data[j] > data[j + 1]) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
return data;
}
/*
* Display function for Bubble Sort.
*/
public static void display(int filesize, int[] data) {
for (int i = 0; i < filesize; i++) {
System.out.print(data[i] + ", ");
}
System.out.println("\n");
}
}