Bubble sort fix (#2809)

This commit is contained in:
Grzegorz Piwowarek 2017-10-22 14:17:45 +02:00 committed by GitHub
parent 71de9142f0
commit a7282d233b
1 changed files with 3 additions and 1 deletions

View File

@ -19,11 +19,13 @@ public class BubbleSort {
void optimizedBubbleSort(Integer[] arr) {
int i = 0, n = arr.length;
boolean swapNeeded = true;
while (i < n - 1 && swapNeeded) {
swapNeeded = false;
for (int j = i + 1; j < n - i; j++) {
for (int j = 1; j < n - i; j++) {
if (arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;