-
Notifications
You must be signed in to change notification settings - Fork 1
/
BubbleSort.java
36 lines (32 loc) · 998 Bytes
/
BubbleSort.java
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
public class BubbleSort {
public static void main(String[] args) {
BubbleSort b = new BubbleSort ();
int[] specimens = t.create();
b.print(specimens);
b.print(b.bubbleSort(specimens));
}
public int[] create() {
int[] j = new int[10];
for (int i = 0; i < j.length; i++) {
j[i] = (int) Math.round(Math.random() * 100);
}
return j;
}
public void print(int input[]) {
for (int i : input) System.out.print(i + " ");
System.out.println();
}
private void swap(int[] specimens, int index) {
int temp = specimens[index - 1];
specimens[index - 1] = specimens[index];
specimens[index] = temp;
}
public int[] bubbleSort(int input[]) {
for (int i = 1; i < input.length; i++) {
for (int j = input.length - 1; j >= i; j--) {
if (input[j - 1] > input[j]) swap(input, j);
}
}
return input;
}
}