diff --git a/src/algorithms/sorting/bubble-sort/BubbleSort.js b/src/algorithms/sorting/bubble-sort/BubbleSort.js index 2da40d56..a57e9ce4 100644 --- a/src/algorithms/sorting/bubble-sort/BubbleSort.js +++ b/src/algorithms/sorting/bubble-sort/BubbleSort.js @@ -7,13 +7,13 @@ export default class BubbleSort extends Sort { // Clone original array to prevent its modification. const array = originalArray.slice(0); - for (let i = 0; i < array.length; i += 1) { + for (let i = 1; i < array.length; i += 1) { swapped = false; // Call visiting callback. this.callbacks.visitingCallback(array[i]); - for (let j = 0; j < array.length - 1; j += 1) { + for (let j = 0; j < array.length - i; j += 1) { // Call visiting callback. this.callbacks.visitingCallback(array[j]); diff --git a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js index 8ab3984e..5ffd6529 100644 --- a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js +++ b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js @@ -9,8 +9,8 @@ import { // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 20; -const NOT_SORTED_ARRAY_VISITING_COUNT = 280; -const REVERSE_SORTED_ARRAY_VISITING_COUNT = 400; +const NOT_SORTED_ARRAY_VISITING_COUNT = 189; +const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209; const EQUAL_ARRAY_VISITING_COUNT = 20; describe('BubbleSort', () => {