-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-sort.java
37 lines (26 loc) · 874 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
37
public class Main{
public static void bubbleSort(int array[]){
for(int turn=0; turn<array.length-1; turn++){
for(int j=0; j<array.length-1-turn; j++){
if(array[j]>array[j+1]){
//swap
int temp = array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
public static void printArray(int array[]){
for(int i=0; i<array.length; i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static void main(String[]args){
int array[]={2, 3, 1, 4, 5};
bubbleSort(array);
System.out.println("Sorted array is");
printArray(array);
}
}