-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSorterSimple.java
More file actions
31 lines (25 loc) · 931 Bytes
/
BubbleSorterSimple.java
File metadata and controls
31 lines (25 loc) · 931 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
package sort;
import java.util.Arrays;
public class BubbleSorterSimple {
public static void main(String[] args) {
int[] data = {8,7,6,4,2};
new BubbleSorterSimple().sort(data);
System.out.println(Arrays.toString(data));
}
public void sort(int[] data) {
/* In the pseudo code we have 'for i = 0 to A.length-2'
* Both values 0 and A.length-2 are inclusive, so in the java code below
* we are having the condition i < data.length - 1
* if you want you can change it to i <= data.length - 2 */
for (int i = 0; i < data.length - 1; i++) {
for (int j = 0; j < data.length - 1 - i; j++) {
// do the swap if required
if (data[j] > data[j+1]) {
int tmp = data[j+1];
data[j+1] = data[j];
data[j] = tmp;
}
}
}
}
}