From 43090bee2a8ec873a7028d96d9a9bd953a7cbe2c Mon Sep 17 00:00:00 2001 From: SidKwok <oceankwok@hotmail.com> Date: Sun, 21 Oct 2018 10:21:09 +0800 Subject: [PATCH 1/2] InsertionSort: use index to compare instead `undefined` --- src/algorithms/sorting/insertion-sort/InsertionSort.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/algorithms/sorting/insertion-sort/InsertionSort.js b/src/algorithms/sorting/insertion-sort/InsertionSort.js index 472625480a..36cf5c0a66 100644 --- a/src/algorithms/sorting/insertion-sort/InsertionSort.js +++ b/src/algorithms/sorting/insertion-sort/InsertionSort.js @@ -14,16 +14,14 @@ 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; From f8a0ec8012f9aa736b3e2b094dc4d3afed600fee Mon Sep 17 00:00:00 2001 From: SidKwok <oceankwok@hotmail.com> Date: Sun, 21 Oct 2018 10:38:31 +0800 Subject: [PATCH 2/2] InserationSort: fix lint --- src/algorithms/sorting/insertion-sort/InsertionSort.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/algorithms/sorting/insertion-sort/InsertionSort.js b/src/algorithms/sorting/insertion-sort/InsertionSort.js index 36cf5c0a66..7b4e7bbf34 100644 --- a/src/algorithms/sorting/insertion-sort/InsertionSort.js +++ b/src/algorithms/sorting/insertion-sort/InsertionSort.js @@ -21,7 +21,8 @@ export default class InsertionSort extends Sort { this.callbacks.visitingCallback(array[currentIndex - 1]); // Swap the elements. - [array[currentIndex - 1], array[currentIndex]] = [array[currentIndex], array[currentIndex - 1]] + [array[currentIndex - 1], array[currentIndex]] = [ + array[currentIndex], array[currentIndex - 1]]; // Shift current index left. currentIndex -= 1;