diff --git a/src/algorithms/sorting/insertion-sort/InsertionSort.js b/src/algorithms/sorting/insertion-sort/InsertionSort.js index 472625480a..7b4e7bbf34 100644 --- a/src/algorithms/sorting/insertion-sort/InsertionSort.js +++ b/src/algorithms/sorting/insertion-sort/InsertionSort.js @@ -14,16 +14,15 @@ export default class InsertionSort extends Sort { // Go and check if previous elements and greater then current one. // If this is the case then swap that elements. while ( - array[currentIndex - 1] !== undefined + currentIndex - 1 >= 0 && this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) ) { // Call visiting callback. this.callbacks.visitingCallback(array[currentIndex - 1]); // Swap the elements. - const tmp = array[currentIndex - 1]; - array[currentIndex - 1] = array[currentIndex]; - array[currentIndex] = tmp; + [array[currentIndex - 1], array[currentIndex]] = [ + array[currentIndex], array[currentIndex - 1]]; // Shift current index left. currentIndex -= 1;