-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
26 lines (22 loc) · 682 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
26 lines (22 loc) · 682 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
package Sorting;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] array = {17, -4, 23, 0, -15, 8, 3, -9, 12, -1, 6, -20, 14, 5, -7, 19, -2, 11, -13, 2};
System.out.println(Arrays.toString(bubble(array)));
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int[] bubble(int [] arr){
for (int i = 0; i < arr.length-1; i++) {
for(int j = 0 ; j < arr.length-i-1 ; j++){
if(arr[j]>arr[j+1])
swap(arr, j , j+1);
}
}
return arr;
}
}