From df436633ce402c5380b28290862beacf6bb20ff4 Mon Sep 17 00:00:00 2001 From: justin Date: Sat, 14 Jan 2023 23:23:15 +0800 Subject: [PATCH] Update JavaScript and TypeScript code style (Chapter of Sorting) --- .../javascript/chapter_sorting/bubble_sort.js | 20 ++-- .../chapter_sorting/insertion_sort.js | 6 +- .../javascript/chapter_sorting/merge_sort.js | 12 +- .../javascript/chapter_sorting/quick_sort.js | 103 ++++++++---------- .../typescript/chapter_sorting/merge_sort.ts | 4 +- docs/chapter_sorting/quick_sort.md | 36 +++--- 6 files changed, 87 insertions(+), 94 deletions(-) diff --git a/codes/javascript/chapter_sorting/bubble_sort.js b/codes/javascript/chapter_sorting/bubble_sort.js index 3d3c6a83b..6ddb0a17b 100644 --- a/codes/javascript/chapter_sorting/bubble_sort.js +++ b/codes/javascript/chapter_sorting/bubble_sort.js @@ -11,10 +11,10 @@ function bubbleSort(nums) { // 内循环:冒泡操作 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { - // 交换 nums[j] 与 nums[j + 1] - let tmp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tmp; + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; } } } @@ -40,10 +40,10 @@ function bubbleSortWithFlag(nums) { } /* Driver Code */ -var nums = [4, 1, 3, 1, 5, 2] -bubbleSort(nums) -console.log("排序后数组 nums =", nums) +const nums = [4, 1, 3, 1, 5, 2]; +bubbleSort(nums); +console.log("排序后数组 nums =", nums); -var nums1 = [4, 1, 3, 1, 5, 2] -bubbleSortWithFlag(nums1) -console.log("排序后数组 nums =", nums1) +const nums1 = [4, 1, 3, 1, 5, 2]; +bubbleSortWithFlag(nums1); +console.log("排序后数组 nums =", nums1); diff --git a/codes/javascript/chapter_sorting/insertion_sort.js b/codes/javascript/chapter_sorting/insertion_sort.js index 02c45a237..66264d234 100644 --- a/codes/javascript/chapter_sorting/insertion_sort.js +++ b/codes/javascript/chapter_sorting/insertion_sort.js @@ -19,6 +19,6 @@ function insertionSort(nums) { } /* Driver Code */ -var nums = [4, 1, 3, 1, 5, 2] -insertionSort(nums) -console.log("排序后数组 nums =", nums) +const nums = [4, 1, 3, 1, 5, 2]; +insertionSort(nums); +console.log('排序后数组 nums =', nums); diff --git a/codes/javascript/chapter_sorting/merge_sort.js b/codes/javascript/chapter_sorting/merge_sort.js index b00c17bcd..4e2449af2 100644 --- a/codes/javascript/chapter_sorting/merge_sort.js +++ b/codes/javascript/chapter_sorting/merge_sort.js @@ -5,10 +5,10 @@ */ /** -* 合并左子数组和右子数组 -* 左子数组区间 [left, mid] -* 右子数组区间 [mid + 1, right] -*/ + * 合并左子数组和右子数组 + * 左子数组区间 [left, mid] + * 右子数组区间 [mid + 1, right] + */ function merge(nums, left, mid, right) { // 初始化辅助数组 let tmp = nums.slice(left, right + 1); @@ -46,6 +46,6 @@ function mergeSort(nums, left, right) { } /* Driver Code */ -var nums = [ 7, 3, 2, 6, 0, 1, 5, 4 ] +const nums = [ 7, 3, 2, 6, 0, 1, 5, 4 ] mergeSort(nums, 0, nums.length - 1) -console.log("归并排序完成后 nums =", nums) +console.log('归并排序完成后 nums =', nums) diff --git a/codes/javascript/chapter_sorting/quick_sort.js b/codes/javascript/chapter_sorting/quick_sort.js index 405bf7621..47ac100fd 100644 --- a/codes/javascript/chapter_sorting/quick_sort.js +++ b/codes/javascript/chapter_sorting/quick_sort.js @@ -8,38 +8,38 @@ class QuickSort { /* 元素交换 */ swap(nums, i, j) { - let tmp = nums[i] - nums[i] = nums[j] - nums[j] = tmp + let tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; } /* 哨兵划分 */ - partition(nums, left, right){ + partition(nums, left, right) { // 以 nums[left] 作为基准数 - let i = left, j = right - while(i < j){ - while(i < j && nums[j] >= nums[left]){ - j -= 1 // 从右向左找首个小于基准数的元素 + let i = left, j = right; + while (i < j) { + while (i < j && nums[j] >= nums[left]) { + j -= 1; // 从右向左找首个小于基准数的元素 } - while(i < j && nums[i] <= nums[left]){ - i += 1 // 从左向右找首个大于基准数的元素 + while (i < j && nums[i] <= nums[left]) { + i += 1; // 从左向右找首个大于基准数的元素 } // 元素交换 - this.swap(nums, i, j) // 交换这两个元素 + this.swap(nums, i, j); // 交换这两个元素 } - this.swap(nums, i, left) // 将基准数交换至两子数组的分界线 - return i // 返回基准数的索引 + this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 + return i; // 返回基准数的索引 } /* 快速排序 */ - quickSort(nums, left, right){ + quickSort(nums, left, right) { // 子数组长度为 1 时终止递归 - if(left >= right) return + if (left >= right) return; // 哨兵划分 - const pivot = this.partition(nums, left, right) + const pivot = this.partition(nums, left, right); // 递归左子数组、右子数组 - this.quickSort(nums, left, pivot - 1) - this.quickSort(nums, pivot + 1, right) + this.quickSort(nums, left, pivot - 1); + this.quickSort(nums, pivot + 1, right); } } @@ -47,21 +47,18 @@ class QuickSort { class QuickSortMedian { /* 元素交换 */ swap(nums, i, j) { - let tmp = nums[i] - nums[i] = nums[j] - nums[j] = tmp + let tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; } /* 选取三个元素的中位数 */ medianThree(nums, left, mid, right) { // 使用了异或操作来简化代码 // 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right])) - return left; - else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) - return mid; - else - return right; + if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right])) return left; + else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; + else return right; } /* 哨兵划分(三数取中值) */ @@ -73,14 +70,12 @@ class QuickSortMedian { // 以 nums[left] 作为基准数 let i = left, j = right; while (i < j) { - while (i < j && nums[j] >= nums[left]) - j--; // 从右向左找首个小于基准数的元素 - while (i < j && nums[i] <= nums[left]) - i++; // 从左向右找首个大于基准数的元素 + while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素 + while (i < j && nums[i] <= nums[left]) i++; // 从左向右找首个大于基准数的元素 this.swap(nums, i, j); // 交换这两个元素 } - this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 - return i; // 返回基准数的索引 + this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 + return i; // 返回基准数的索引 } /* 快速排序 */ @@ -99,9 +94,9 @@ class QuickSortMedian { class QuickSortTailCall { /* 元素交换 */ swap(nums, i, j) { - let tmp = nums[i] - nums[i] = nums[j] - nums[j] = tmp + let tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; } /* 哨兵划分 */ @@ -109,13 +104,11 @@ class QuickSortTailCall { // 以 nums[left] 作为基准数 let i = left, j = right; while (i < j) { - while (i < j && nums[j] >= nums[left]) - j--; // 从右向左找首个小于基准数的元素 - while (i < j && nums[i] <= nums[left]) - i++; // 从左向右找首个大于基准数的元素 + while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素 + while (i < j && nums[i] <= nums[left]) i++; // 从左向右找首个大于基准数的元素 this.swap(nums, i, j); // 交换这两个元素 } - this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 + this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 return i; // 返回基准数的索引 } @@ -127,8 +120,8 @@ class QuickSortTailCall { let pivot = this.partition(nums, left, right); // 对两个子数组中较短的那个执行快排 if (pivot - left < right - pivot) { - this.quickSort(nums, left, pivot - 1); // 递归排序左子数组 - left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right] + this.quickSort(nums, left, pivot - 1); // 递归排序左子数组 + left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right] } else { this.quickSort(nums, pivot + 1, right); // 递归排序右子数组 right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1] @@ -139,19 +132,19 @@ class QuickSortTailCall { /* Driver Code */ /* 快速排序 */ -var nums = [4, 1, 3, 1, 5, 2] -var quickSort = new QuickSort() -quickSort.quickSort(nums, 0, nums.length - 1) -console.log("快速排序完成后 nums =", nums) +const nums = [4, 1, 3, 1, 5, 2]; +const quickSort = new QuickSort(); +quickSort.quickSort(nums, 0, nums.length - 1); +console.log('快速排序完成后 nums =', nums); /* 快速排序(中位基准数优化) */ -nums1 = [4, 1, 3, 1, 5,2] -var quickSortMedian = new QuickSort() -quickSortMedian.quickSort(nums1, 0, nums1.length - 1) -console.log("快速排序(中位基准数优化)完成后 nums =", nums1) +const nums1 = [4, 1, 3, 1, 5, 2]; +const quickSortMedian = new QuickSort(); +quickSortMedian.quickSort(nums1, 0, nums1.length - 1); +console.log('快速排序(中位基准数优化)完成后 nums =', nums1); /* 快速排序(尾递归优化) */ -nums2 = [4, 1, 3, 1, 5, 2] -var quickSortTailCall = new QuickSort() -quickSortTailCall.quickSort(nums2, 0, nums2.length - 1) -console.log("快速排序(尾递归优化)完成后 nums =", nums2) +const nums2 = [4, 1, 3, 1, 5, 2]; +const quickSortTailCall = new QuickSort(); +quickSortTailCall.quickSort(nums2, 0, nums2.length - 1); +console.log('快速排序(尾递归优化)完成后 nums =', nums2); diff --git a/codes/typescript/chapter_sorting/merge_sort.ts b/codes/typescript/chapter_sorting/merge_sort.ts index a4cb5e1cb..754502949 100644 --- a/codes/typescript/chapter_sorting/merge_sort.ts +++ b/codes/typescript/chapter_sorting/merge_sort.ts @@ -23,10 +23,10 @@ function merge(nums: number[], left: number, mid: number, right: number): void { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) { nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if (j > rightEnd || tmp[i] <= tmp[j]) { nums[k] = tmp[i++]; - // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j++]; } diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index a712a1bff..62ab2147f 100644 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -134,27 +134,27 @@ comments: true ``` js title="quick_sort.js" /* 元素交换 */ function swap(nums, i, j) { - let tmp = nums[i] - nums[i] = nums[j] - nums[j] = tmp + let tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; } /* 哨兵划分 */ - function partition(nums, left, right){ + function partition(nums, left, right) { // 以 nums[left] 作为基准数 - let i = left, j = right - while(i < j){ - while(i < j && nums[j] >= nums[left]){ - j -= 1 // 从右向左找首个小于基准数的元素 + let i = left, j = right; + while (i < j) { + while (i < j && nums[j] >= nums[left]) { + j -= 1; // 从右向左找首个小于基准数的元素 } - while(i < j && nums[i] <= nums[left]){ - i += 1 // 从左向右找首个大于基准数的元素 + while (i < j && nums[i] <= nums[left]) { + i += 1; // 从左向右找首个大于基准数的元素 } // 元素交换 - swap(nums, i, j) // 交换这两个元素 + swap(nums, i, j); // 交换这两个元素 } - swap(nums, i, left) // 将基准数交换至两子数组的分界线 - return i // 返回基准数的索引 + swap(nums, i, left); // 将基准数交换至两子数组的分界线 + return i; // 返回基准数的索引 } ``` @@ -313,14 +313,14 @@ comments: true ```js title="quick_sort.js" /* 快速排序 */ - function quickSort(nums, left, right){ + function quickSort(nums, left, right) { // 子数组长度为 1 时终止递归 - if(left >= right) return + if (left >= right) return; // 哨兵划分 - const pivot = partition(nums, left, right) + const pivot = partition(nums, left, right); // 递归左子数组、右子数组 - quick_sort(nums, left, pivot - 1) - quick_sort(nums, pivot + 1, right) + quickSort(nums, left, pivot - 1); + quickSort(nums, pivot + 1, right); } ```