-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.java
More file actions
31 lines (29 loc) · 774 Bytes
/
Copy pathbubble.java
File metadata and controls
31 lines (29 loc) · 774 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 sorting;
public class bubble {
public static int[] bub(int arr[]){
int temp;
for (int i = 0; i < arr.length-1; i++) {
int swap = 0;
for (int j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swap++;
}
}
if(swap == 0){
break;
}
}
return arr;
}
public static void main(String[] args) {
int arr[] = {4, 6, 2, 9};
arr = bub(arr);
System.out.print("Sorted array: ");
for (int i : arr) {
System.out.print(i + " ");
}
}
}