committed by
GitHub
parent
e5400faad8
commit
f753a86e84
@@ -4,22 +4,20 @@ import java.util.stream.IntStream;
|
||||
|
||||
public class BubbleSort {
|
||||
|
||||
public void bubbleSort(Integer[] arr) {
|
||||
void bubbleSort(Integer[] arr) {
|
||||
int n = arr.length;
|
||||
IntStream.range(0, n - 1)
|
||||
.forEach(i -> {
|
||||
IntStream.range(i + 1, n - i)
|
||||
.forEach(j -> {
|
||||
if (arr[j - 1] > arr[j]) {
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j - 1];
|
||||
arr[j - 1] = temp;
|
||||
}
|
||||
});
|
||||
});
|
||||
.flatMap(i -> IntStream.range(i + 1, n - i))
|
||||
.forEach(j -> {
|
||||
if (arr[j - 1] > arr[j]) {
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j - 1];
|
||||
arr[j - 1] = temp;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void optimizedBubbleSort(Integer[] arr) {
|
||||
void optimizedBubbleSort(Integer[] arr) {
|
||||
int i = 0, n = arr.length;
|
||||
boolean swapNeeded = true;
|
||||
while (i < n - 1 && swapNeeded) {
|
||||
@@ -37,5 +35,4 @@ public class BubbleSort {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user