Skip to content

Commit

Permalink
Merge pull request krahets#257 from justin-tse/sort
Browse files Browse the repository at this point in the history
Update JavaScript and TypeScript code style for Chapter of Sorting
  • Loading branch information
krahets authored Jan 14, 2023
2 parents 4bae839 + df43663 commit 33e84ff
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 94 deletions.
20 changes: 10 additions & 10 deletions codes/javascript/chapter_sorting/bubble_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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);
6 changes: 3 additions & 3 deletions codes/javascript/chapter_sorting/insertion_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
12 changes: 6 additions & 6 deletions codes/javascript/chapter_sorting/merge_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
103 changes: 48 additions & 55 deletions codes/javascript/chapter_sorting/quick_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,57 @@
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);
}
}

/* 快速排序类(中位基准数优化) */
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;
}

/* 哨兵划分(三数取中值) */
Expand All @@ -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; // 返回基准数的索引
}

/* 快速排序 */
Expand All @@ -99,23 +94,21 @@ 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;
}

/* 哨兵划分 */
partition(nums, left, right) {
// 以 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; // 返回基准数的索引
}

Expand All @@ -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]
Expand All @@ -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);
4 changes: 2 additions & 2 deletions codes/typescript/chapter_sorting/merge_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++];
}
Expand Down
36 changes: 18 additions & 18 deletions docs/chapter_sorting/quick_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 返回基准数的索引
}
```

Expand Down Expand Up @@ -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);
}
```

Expand Down

0 comments on commit 33e84ff

Please sign in to comment.