-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
38 lines (37 loc) · 1.04 KB
/
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
34
35
36
37
38
package codingProblems;
public class BubbleSort {
public static void main(String args[])
{
int[] arr = {10,6,8,23,6};
BubbleSortMethod(arr);
for(int n:arr)
System.out.print(n+" ");
}
public static void BubbleSortMethod(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 1; j < array.length - i; j++) {
if (array[j - 1] > array[j]) {
swap(array, j, j - 1);
}
}
}
}
public static void sort(int[] array) {
int unsortedBelow = array.length;
while (unsortedBelow != 0) {
int lastSwap = 0;
for (int i = 1; i < unsortedBelow; i++) {
if (array[i - 1] > array[i]) {
swap(array, i, i - 1);
lastSwap = i;
}
}
unsortedBelow = lastSwap;
}
}
private static void swap(int[] array, int a, int b) {
Integer temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}