From f08002ed2a01cc6b9a8717cef46221a4b95fe18b Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 16:57:36 +0800 Subject: [PATCH 1/3] refactor: optimizing JavaScript examples --- basic/sorting/BubbleSort/README.md | 36 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index b28e7ae4e4ec3..95fff1d733a8d 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -45,25 +45,27 @@ public class BubbleSort { ```js function bubbleSort(inputArr) { - let len = inputArr.length; - let swapped = false; - for (let i = 1; i <= len - 1; i++) { - swapped = false; - for (let j = 0; j < len - 1; j++) { - if (inputArr[j] > inputArr[j + 1]) { - let temp = inputArr[j]; - inputArr[j] = inputArr[j + 1]; - inputArr[j + 1] = temp; - swapped = true - } - } - if (swapped === false) break; - } - return (inputArr) + for (let i = inputArr.length - 1; i > 0; i--) { + let hasChange = false; + for (let j = 0; j < i; j++) { + if (inputArr[j] > inputArr[j + 1]) { + const temp = inputArr[j]; + inputArr[j] = inputArr[j + 1]; + inputArr[j + 1] = temp; + hasChange = true; + } + } + + if (!hasChange) { + break; + } + } + + return inputArr; } -let arr = [6, 3, 2, 1, 5]; -console.log(bubbleSort(arr)) +const arr = [6, 3, 2, 1, 5]; +console.log(bubbleSort(arr)); ``` ### **Go** From 21b6cd3b2e38189d885c4a7a1d03f1656b822013 Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 17:46:58 +0800 Subject: [PATCH 2/3] style: adjust code indentation --- basic/sorting/BubbleSort/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index 95fff1d733a8d..94101fe44eb82 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -60,7 +60,7 @@ function bubbleSort(inputArr) { break; } } - + return inputArr; } From 6628ba95f15441c04a45a2fe9370211246264a4f Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 18:09:30 +0800 Subject: [PATCH 3/3] style: adjust code indentation --- basic/sorting/BubbleSort/README.md | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index 94101fe44eb82..03d25c487c5d9 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -45,23 +45,23 @@ public class BubbleSort { ```js function bubbleSort(inputArr) { - for (let i = inputArr.length - 1; i > 0; i--) { - let hasChange = false; - for (let j = 0; j < i; j++) { - if (inputArr[j] > inputArr[j + 1]) { - const temp = inputArr[j]; - inputArr[j] = inputArr[j + 1]; - inputArr[j + 1] = temp; - hasChange = true; - } - } - - if (!hasChange) { - break; - } - } - - return inputArr; + for (let i = inputArr.length - 1; i > 0; i--) { + let hasChange = false; + for (let j = 0; j < i; j++) { + if (inputArr[j] > inputArr[j + 1]) { + const temp = inputArr[j]; + inputArr[j] = inputArr[j + 1]; + inputArr[j + 1] = temp; + hasChange = true; + } + } + + if (!hasChange) { + break; + } + } + + return inputArr; } const arr = [6, 3, 2, 1, 5];