-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
56 lines (52 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// DO NOT USE
public class BubbleSort
{
public static void sort(int[] arr) {bSort(arr,0,arr.length-1,false);}
public static void sort(int[] arr, boolean rev) {bSort(arr,0,arr.length-1,rev);}
private static void bSort(int[] arr, int start, int end, boolean rev)
{
if(rev)
{
while(!check(arr,start,end,rev))
{
for(int i = start; i < end; i++)
{
if(arr[i] < arr[i+1])
{
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
while(!check(arr,start,end,rev))
{
for(int i = start; i < end; i++)
{
if(arr[i] > arr[i+1])
{
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
private static boolean check(int[] arr, int start, int end, boolean rev)
{
if(rev)
{
for(int i = start; i < end; i++)
{
if(arr[i] < arr[i+1]) return false;
}
return true;
}
for(int i = start; i < end; i++)
{
if(arr[i] > arr[i+1]) return false;
}
return true;
}
}