-
Notifications
You must be signed in to change notification settings - Fork 127
/
bubble-sort.java
36 lines (33 loc) · 901 Bytes
/
bubble-sort.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
import java.util.Arrays;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int len=in.nextInt();
int[] arr=new int[len];
for(int i=0;i<len;i++){
arr[i]=in.nextInt();
}
System.out.println(Arrays.toString(arr));
sorting(arr,len);
System.out.println(Arrays.toString(arr));
}
static void sorting(int[] arr,int len){
int temp;
boolean swapped;
for(int i=0;i<len;i++){
swapped=false;
for(int j=1;j<=len-1-i;j++){
if(arr[j-1]>arr[j]){
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
swapped=true;
}
}
if(!swapped){
break;
}
}
}
}