-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.java
55 lines (49 loc) · 1.96 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
public class BubbleSort {
public static void bubbleSort(int[]a){ // dana's code i was absent
for (int j=0;j<a.length-1;j++){
for(int i=0;i<a.length-1-j;i++){ //-j because no need to loop over sorted elemnts extra work
if(a[i]>a[i+1]){
int temp=a[i+1];
a[i+1]=a[i];
a[i]=temp;
}
}
}
}
public static void BubbleSortDecreasing(int a[]){ // my code
for (int j = 0 ; j < a.length - 1 ; j++){
for(int i = 0 ; i < a.length - 1 - j ; i++){
if(a[i] < a[i + 1]){
int temp = a[i + 1];
a[i + 1] = a[i];
a[i] = temp;
}
}
}
}
public static void BestCaseBubbleSort (int a[]){
// to check if there was any swapping, if there was no swapping then it means the array is srted
boolean swap = false; // initialize swap to false because we assume there were no swaps done... then we check :)
for (int j = 0; j < a.length - 1; j++) { // controls the number of iterations through the array
boolean flag = false; // set flag
for (int i = 0; i < a.length - 1 - j; i++) { // swaps elements that are next to each other
if (a[i] > a[i + 1]) {
int temp = a[i + 1];
a[i + 1] = a[i];
a[i] = temp;
swap = true;
}
}
if (!swap) {
break; // if there was no swapping then it means the array is srted
}
}
}
public static void main(String args[]){
int arr[] = {2,5,8,3,7,1,9,4,6,0};
BestCaseBubbleSort(arr); // call any function, mine or dana's, mine is decreasing , dana's increasing
for(int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
}
}