diff --git a/src/algorithms/sorting/insertion-sort/InsertionSort.js b/src/algorithms/sorting/insertion-sort/InsertionSort.js index aa447542..7a801613 100644 --- a/src/algorithms/sorting/insertion-sort/InsertionSort.js +++ b/src/algorithms/sorting/insertion-sort/InsertionSort.js @@ -5,14 +5,14 @@ export default class InsertionSort extends Sort { const array = [...originalArray]; // Go through all array elements... - for (let i = 0; i < array.length; i += 1) { + for (let i = 1; i < array.length; i += 1) { let currentIndex = i; // Call visiting callback. this.callbacks.visitingCallback(array[i]); - // Go and check if previous elements and greater then current one. - // If this is the case then swap that elements. + // Check if previous element is greater than current element. + // If so, swap the two elements. while ( array[currentIndex - 1] !== undefined && this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) diff --git a/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js b/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js index 588472e2..8453b7db 100644 --- a/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js +++ b/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js @@ -8,10 +8,10 @@ import { } from '../../SortTester'; // Complexity constants. -const SORTED_ARRAY_VISITING_COUNT = 20; -const NOT_SORTED_ARRAY_VISITING_COUNT = 101; -const REVERSE_SORTED_ARRAY_VISITING_COUNT = 210; -const EQUAL_ARRAY_VISITING_COUNT = 20; +const SORTED_ARRAY_VISITING_COUNT = 19; +const NOT_SORTED_ARRAY_VISITING_COUNT = 100; +const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209; +const EQUAL_ARRAY_VISITING_COUNT = 19; describe('InsertionSort', () => { it('should sort array', () => {