-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.java
34 lines (33 loc) · 926 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
class BubbleSort{
//In every case it will sort with 0(n^2) complexity
public static Integer[] sort(Integer[] list){
for(int i=0;i<list.length;i++){
for(int j=0;j<list.length-1;j++){
if(list[j]>list[j+1]){
int temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
return list;
}
//if array is sorted then this will sort with 0(n) complexity
public static Integer[] sortWithMinialComplexity(Integer[] list){
for(int i=0;i<list.length;i++){
boolean swaped=false;
for(int j=0;j<list.length-i-1;j++){
if(list[j]>list[j+1]){
//swap
int v=list[j];
list[j]=list[j+1];
list[j+1]=v;
swaped=true;
}
}
if(!swaped){
break;
}
}
return list;
}