-
Notifications
You must be signed in to change notification settings - Fork 91
/
BubbleSort.java
33 lines (26 loc) · 904 Bytes
/
BubbleSort.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
package com.company;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {-5, 83, 1, 0};
bubble(arr);
System.out.println(Arrays.toString(arr));
}
static void bubble(int[] arr) {
boolean swap;
for (int i = 0; i < arr.length ; i++) { //parsing through length of the array
swap = false;
for (int j = 1; j < arr.length - i ; j++) { //comparing the elements
if(arr[j] < arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
swap = true;
}
}
if(!swap){ //if swapping of elements is not executing, swap = false and break the loop to save time
break;
}
}
}
}