-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathbubble_sort.java
More file actions
31 lines (29 loc) · 741 Bytes
/
bubble_sort.java
File metadata and controls
31 lines (29 loc) · 741 Bytes
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
package dsa;
import java.util.Arrays;
public class bubble_sort {
public static void main(String[] args) {
int[] arr={-12,12,45,-67,-1,-99,-94,0,-3,99};
sort(arr);
System.out.println(Arrays.toString(arr));
}
static void sort(int[] arr){
// boolean swap=false;
int i=1;
int a=arr.length;
while(i<a){
boolean swap=false;
for(int j=0;j<a-i;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
swap=true;
}
}
if(!swap){
break;
}
i=i+1;
}
}
}