diff --git a/Data-Structures/Array/QuickSelect.js b/Data-Structures/Array/QuickSelect.js index 473a577a9..7a90d2fcf 100644 --- a/Data-Structures/Array/QuickSelect.js +++ b/Data-Structures/Array/QuickSelect.js @@ -59,9 +59,7 @@ function getRandomInt (min, max) { } function Swap (arr, x, y) { - const temp = arr[x] - arr[x] = arr[y] - arr[y] = temp + [arr[x], arr[y]] = [arr[y], arr[x]] } // testing diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 72c65dda1..3232991d1 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -12,9 +12,7 @@ function bubbleSort (items) { // Compare the adjacent positions if (items[j] < items[j - 1]) { // Swap the numbers - const tmp = items[j] - items[j] = items[j - 1] - items[j - 1] = tmp + [items[j], items[j - 1]] = [items[j - 1], items[j]] } } } @@ -47,9 +45,7 @@ function alternativeBubbleSort (arr) { swapped = false for (let i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { - const temp = arr[i] - arr[i] = arr[i + 1] - arr[i + 1] = temp + [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]] swapped = true } } diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index d447dc77f..337fa5f9f 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -7,14 +7,12 @@ function cocktailShakerSort (items) { for (let i = items.length - 1; i > 0; i--) { let swapped = false - let temp, j + let j // backwards for (j = items.length - 1; j > i; j--) { if (items[j] < items[j - 1]) { - temp = items[j] - items[j] = items[j - 1] - items[j - 1] = temp + [items[j], items[j - 1]] = [items[j - 1], items[j]] swapped = true } } @@ -22,9 +20,7 @@ function cocktailShakerSort (items) { // forwards for (j = 0; j < i; j++) { if (items[j] > items[j + 1]) { - temp = items[j] - items[j] = items[j + 1] - items[j + 1] = temp + [items[j], items[j + 1]] = [items[j + 1], items[j]] swapped = true } } diff --git a/Sorts/CombSort.js b/Sorts/CombSort.js index 3241c59f8..ce34ce157 100644 --- a/Sorts/CombSort.js +++ b/Sorts/CombSort.js @@ -33,9 +33,7 @@ function combSort (list) { while (gap + i < list.length) { if (list[i] > list[i + gap]) { - const value = list[i] - list[i] = list[i + gap] - list[i + gap] = value + [list[i], list[i + gap]] = [list[i + gap], list[i]] isSwapped = true } i += 1 diff --git a/Sorts/GnomeSort.js b/Sorts/GnomeSort.js index 79771193c..d8ccd5ce0 100644 --- a/Sorts/GnomeSort.js +++ b/Sorts/GnomeSort.js @@ -14,9 +14,7 @@ function gnomeSort (items) { if (items[i - 1] <= items[i]) { i++ } else { - const temp = items[i] - items[i] = items[i - 1] - items[i - 1] = temp + [items[i], items[i - 1]] = [items[i - 1], items[i]] i = Math.max(1, i - 1) } diff --git a/Sorts/Heapsort.js b/Sorts/Heapsort.js index d250d41ea..6f010eea4 100644 --- a/Sorts/Heapsort.js +++ b/Sorts/Heapsort.js @@ -22,10 +22,7 @@ function heapRoot (input, i) { } function swap (input, indexA, indexB) { - const temp = input[indexA] - - input[indexA] = input[indexB] - input[indexB] = temp + [input[indexA], input[indexB]] = [input[indexB], input[indexA]] } function heapSort (input) { diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index a46b93ac4..38a4bfff2 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -21,9 +21,7 @@ function selectionSort (items) { if (min !== i) { // After each pass, if the current min num != initial min num, exchange the position. // Swap the numbers - var tmp = items[i] - items[i] = items[min] - items[min] = tmp + [items[i], items[min]] = [items[min], [items[i]]] } } }