-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathBubbleSort.java
57 lines (54 loc) · 1.13 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class BubbleSort
{
private int[] array;
private int size;
private int max;
private int min;
public BubbleSort(int size, int max, int min)
{
array = new int[size];
this.size = size;
this.max = max;
this.min = min;
generateArray();
}
public void printArray()
{
for(int num : array)
{
System.out.println(num);
}
System.out.println();
}
public void sort()
{
System.out.println("Sorting");
for(int i = 1; i < size; i++)
{
for(int j = 0; j < size-i; j++)
{
if(array[j] > array[j+1])
{
swap(j, j+1);
}
}
}
}
private void swap(int x, int y)
{
int temp = array[x];
array[x] = array[y];
array[y] = temp;
}
private void generateArray()
{
for(int i = 0; i < size; i++)
{
array[i] = getRandom();
}
}
private int getRandom()
{
return (int)((Math.random() * (max-min+1)) + min);
}
}