From b137efa8efbbe04e2fcf424965c93a389df57712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Sun, 10 Sep 2023 02:10:06 -0400 Subject: [PATCH] modular no-temp swap --- src/bubble.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/bubble.js b/src/bubble.js index c1be395..dc9f46b 100644 --- a/src/bubble.js +++ b/src/bubble.js @@ -1,10 +1,15 @@ +/** +Swaps 2 indices within array (in-place). +*/ +const swap = (arr, i, j) => { + [arr[i], arr[j]] = [arr[j], arr[i]]; +}; + export function bubbleSort(arr) { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { - let temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; + swap(arr, j, j + 1); } } }