From 885be1e46c28506786ccca5bcd18a1a98ecea94b Mon Sep 17 00:00:00 2001 From: IsChristina <315644507@qq.com> Date: Mon, 12 Dec 2022 21:05:36 +0800 Subject: [PATCH 1/3] add remaining code for Array and LinkedList --- .../chapter_array_and_linkedlist/array.js | 52 +++---- .../linked_list.js | 84 ++++++++++ .../chapter_array_and_linkedlist/list.js | 59 ++++++++ .../chapter_array_and_linkedlist/my_list.js | 143 ++++++++++++++++++ codes/javascript/include/ListNode.js | 47 ++++++ .../linked_list.md | 72 ++++++++- docs/chapter_array_and_linkedlist/list.md | 135 ++++++++++++++++- 7 files changed, 558 insertions(+), 34 deletions(-) create mode 100644 codes/javascript/chapter_array_and_linkedlist/linked_list.js create mode 100644 codes/javascript/chapter_array_and_linkedlist/list.js create mode 100644 codes/javascript/chapter_array_and_linkedlist/my_list.js create mode 100755 codes/javascript/include/ListNode.js diff --git a/codes/javascript/chapter_array_and_linkedlist/array.js b/codes/javascript/chapter_array_and_linkedlist/array.js index 4cf1b29..5e24d32 100644 --- a/codes/javascript/chapter_array_and_linkedlist/array.js +++ b/codes/javascript/chapter_array_and_linkedlist/array.js @@ -7,10 +7,10 @@ /* 随机访问元素 */ function randomAccess(nums){ // 在区间 [0, nums.length) 中随机抽取一个数字 - const random_index = Math.floor(Math.random() * nums.length) + const random_index = Math.floor(Math.random() * nums.length); // 获取并返回随机元素 - random_num = nums[random_index] - return random_num + random_num = nums[random_index]; + return random_num; } /* 扩展数组长度 */ @@ -18,13 +18,13 @@ function randomAccess(nums){ // 为了方便学习,本函数将 list 看作是长度不可变的数组 function extend(nums, enlarge){ // 初始化一个扩展长度后的数组 - let res = new Array(nums.length + enlarge).fill(0) + let res = new Array(nums.length + enlarge).fill(0); // 将原数组中的所有元素复制到新数组 for(let i=0; i P -> n1 + let P = n0.next; + let n1 = P.next; + n0.next = n1; +} + +/* 访问链表中索引为 index 的结点 */ +function access(head, index) { + for (let i = 0; i < index; i++) { + head = head.next; + if (!head) + return null; + } + return head; +} + +/* 在链表中查找值为 target 的首个结点 */ +function find(head, target) { + let index = 0; + while (head !== null) { + if (head.val === target) + return index; + head = head.next; + index++; + } + return -1; +} + +/* Driver Code */ + +/* 初始化链表 */ +// 初始化各个结点 +const n0 = new ListNode(1); +const n1 = new ListNode(3); +const n2 = new ListNode(2); +const n3 = new ListNode(5); +const n4 = new ListNode(4); +// 构建引用指向 +n0.next = n1; +n1.next = n2; +n2.next = n3; +n3.next = n4; +console.log("初始化的链表为"); +PrintUtil.printLinkedList(n0); + +/* 插入结点 */ +insert(n0, new ListNode(0)); +console.log("插入结点后的链表为"); +PrintUtil.printLinkedList(n0); + +/* 删除结点 */ +remove(n0); +console.log("删除结点后的链表为"); +PrintUtil.printLinkedList(n0); + +/* 访问结点 */ +var node = access(n0, 3); +console.log("链表中索引 3 处的结点的值 = " + node.val); + +/* 查找结点 */ +var index = find(n0, 2); +console.log("链表中值为 2 的结点的索引 = " + index); + + diff --git a/codes/javascript/chapter_array_and_linkedlist/list.js b/codes/javascript/chapter_array_and_linkedlist/list.js new file mode 100644 index 0000000..2c2c32b --- /dev/null +++ b/codes/javascript/chapter_array_and_linkedlist/list.js @@ -0,0 +1,59 @@ +/** + * File: list.js + * Created Time: 2022-12-12 + * Author: IsChristina (christinaxia77@foxmail.com) + */ + +/* 初始化列表 */ +var list = [1, 3, 2, 5, 4]; +console.log("列表 list = " + list); + +/* 访问元素 */ +var num = list[1]; +console.log("访问索引 1 处的元素,得到 num = " + num); + +/* 更新元素 */ +list[1] = 0; +console.log("将索引 1 处的元素更新为 0 ,得到 list = " + list); + +/* 清空列表 */ +list = []; +console.log("清空列表后 list = " + list); + +/* 尾部添加元素 */ +list.push(1); +list.push(3); +list.push(2); +list.push(5); +list.push(4); +console.log("添加元素后 list = " + list); + +/* 中间插入元素 */ +list.splice(3, 0, 6); +console.log("在索引 3 处插入数字 6 ,得到 list = " + list); + +/* 删除元素 */ +list.splice(3, 1); +console.log("删除索引 3 处的元素,得到 list = " + list); + +/* 通过索引遍历列表 */ +var count = 0; +for (let i = 0; i < list.length; i++) { + count++; +} + +/* 直接遍历列表元素 */ +count = 0; +for (const n of list) { + count++; +} + +/* 拼接两个列表 */ +var list1 = [ 6, 8, 7, 10, 9 ]; +list.push(...list1) +console.log("将列表 list1 拼接到 list 之后,得到 list = " + list); + +/* 排序列表 */ +list.sort((a, b) => a - b); +console.log("排序列表后 list = " + list); + diff --git a/codes/javascript/chapter_array_and_linkedlist/my_list.js b/codes/javascript/chapter_array_and_linkedlist/my_list.js new file mode 100644 index 0000000..0b4066d --- /dev/null +++ b/codes/javascript/chapter_array_and_linkedlist/my_list.js @@ -0,0 +1,143 @@ +/** + * File: my_list.js + * Created Time: 2022-12-12 + * Author: IsChristina (christinaxia77@foxmail.com) + */ + +/* 列表类简易实现 */ +class MyList { + nums; // 数组(存储列表元素) + _capacity = 10; // 列表容量 + _size = 0; // 列表长度(即当前元素数量) + extendRatio = 2; // 每次列表扩容的倍数 + + /* 构造函数 */ + constructor() { + this.nums = new Array(this._capacity); + } + + /* 获取列表长度(即当前元素数量)*/ + get size() { + return this._size; + } + + /* 获取列表容量 */ + get capacity() { + return this._capacity; + } + + /* 访问元素 */ + get(index) { + // 索引如果越界则抛出异常,下同 + if (index >= this._size) + throw new Error("索引越界"); + return this.nums[index]; + } + + /* 更新元素 */ + set(index, num) { + if (index >= this._size) + throw new Error("索引越界"); + this.nums[index] = num; + } + + /* 尾部添加元素 */ + add(num) { + //元素数量超出容量时,触发扩容机制 + if (this._size === this._capacity) + this.extendCapacity(); + this.nums[this._size] = num; + // 更新元素数量 + this._size++; + } + + /* 中间插入元素 */ + insert(index, num) { + if (index >= this._size) + throw new Error("索引越界"); + // 元素数量超出容量时,触发扩容机制 + if (this._size === this._capacity) + this.extendCapacity(); + // 将索引 index 以及之后的元素都向后移动一位 + for (let j = this._size - 1; j >= index; j--) { + this.nums[j + 1] = this.nums[j]; + } + this.nums[index] = num; + // 更新元素数量 + this._size++; + } + + /* 删除元素 */ + remove(index) { + if (index >= this._size) + throw new Error("索引越界"); + let num = this.nums[index]; + // 将索引 index 之后的元素都向前移动一位 + for (let j = index; j < this._size - 1; j++) { + this.nums[j] = this.nums[j + 1]; + } + // 更新元素数量 + this._size--; + // 返回被删除元素 + return num; + } + + /* 列表扩容 */ + extendCapacity() { + // 新建一个长度为 size 的数组,并将原数组拷贝到新数组 + this.nums = this.nums.concat( + new Array(this.capacity * (this.extendRatio - 1)) + ); + // 更新列表容量 + this._capacity = this.nums.length; + } + + /* 将列表转换为数组 */ + toArray() { + let size = this.size; + // 仅转换有效长度范围内的列表元素 + let nums = new Array(size); + for (let i = 0; i < size; i++) { + nums[i] = this.get(i); + } + return nums; + } +} + +/* Driver Code */ + +/* 初始化列表 */ +var list = new MyList(); +/* 尾部添加元素 */ +list.add(1); +list.add(3); +list.add(2); +list.add(5); +list.add(4); +console.log("列表 list = " + list.toArray().toString() + + " ,容量 = " + list.capacity + " ,长度 = " + list.size); + +/* 中间插入元素 */ +list.insert(3, 6); +console.log("在索引 3 处插入数字 6 ,得到 list = " + list.toArray().toString()); + +/* 删除元素 */ +list.remove(3); +console.log("删除索引 3 处的元素,得到 list = " + list.toArray().toString()); + +/* 访问元素 */ +var num = list.get(1); +console.log("访问索引 1 处的元素,得到 num = " + num); + +/* 更新元素 */ +list.set(1, 0); +console.log("将索引 1 处的元素更新为 0 ,得到 list = " + list.toArray().toString()); + +/* 测试扩容机制 */ +for (let i = 0; i < 10; i++) { + // 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 + list.add(i); +} +console.log("扩容后的列表 list = " + list.toArray().toString() + + " ,容量 = " + list.capacity + " ,长度 = " + list.size); + diff --git a/codes/javascript/include/ListNode.js b/codes/javascript/include/ListNode.js new file mode 100755 index 0000000..793915e --- /dev/null +++ b/codes/javascript/include/ListNode.js @@ -0,0 +1,47 @@ +/** + * File: ListNode.js + * Created Time: 2022-12-12 + * Author: IsChristina (christinaxia77@foxmail.com) + */ + +/** + * Definition for a singly-linked list node + */ +class ListNode { + val; + next; + constructor(val, next) { + this.val = (val === undefined ? 0 : val); + this.next = (next === undefined ? null : next); + } + + /** + * Generate a linked list with an array + * @param arr + * @return + */ + arrToLinkedList(arr) { + let dum = new ListNode(0); + let head = dum; + for (const val of arr) { + head.next = new ListNode(val); + head = head.next; + } + return dum.next; + } + + /** + * Get a list node with specific value from a linked list + * @param head + * @param val + * @return + */ + getListNode(head, val) { + while (head !== null && head.val !== val) { + head = head.next; + } + return head; + } +} + +module.exports = ListNode diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 8d1d979..629fcec 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -57,7 +57,15 @@ comments: true === "JavaScript" ```js title="" - + /* 链表结点结构体 */ + class ListNode { + val; + next; + constructor(val, next) { + this.val = (val === undefined ? 0 : val); // 结点值 + this.next = (next === undefined ? null : next); // 指向下一结点的引用 + } + } ``` === "TypeScript" @@ -154,7 +162,18 @@ comments: true === "JavaScript" ```js title="" - + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + const n0 = new ListNode(1); + const n1 = new ListNode(3); + const n2 = new ListNode(2); + const n3 = new ListNode(5); + const n4 = new ListNode(4); + // 构建引用指向 + n0.next = n1; + n1.next = n2; + n2.next = n3; + n3.next = n4; ``` === "TypeScript" @@ -264,7 +283,22 @@ comments: true === "JavaScript" ```js title="" + /* 在链表的结点 n0 之后插入结点 P */ + function insert(n0, P) { + let n1 = n0.next; + n0.next = P; + P.next = n1; + } + /* 删除链表的结点 n0 之后的首个结点 */ + function remove(n0) { + if (!n0.next) + return; + // n0 -> P -> n1 + let P = n0.next; + let n1 = P.next; + n0.next = n1; + } ``` === "TypeScript" @@ -353,7 +387,15 @@ comments: true === "JavaScript" ```js title="" - + /* 访问链表中索引为 index 的结点 */ + function access(head, index) { + for (let i = 0; i < index; i++) { + head = head.next; + if (!head) + return null; + } + return head; + } ``` === "TypeScript" @@ -444,7 +486,17 @@ comments: true === "JavaScript" ```js title="" - + /* 在链表中查找值为 target 的首个结点 */ + function find(head, target) { + let index = 0; + while (head !== null) { + if (head.val === target) + return index; + head = head.next; + index++; + } + return -1; + } ``` === "TypeScript" @@ -528,7 +580,17 @@ comments: true === "JavaScript" ```js title="" - + /* 双向链表结点类 */ + class ListNode { + val; + next; + prev; + constructor(val, next) { + this.val = val === undefined ? 0 : val; // 结点值 + this.next = next === undefined ? null : next; // 指向后继结点的引用 + this.prev = prev === undefined ? null : prev; // 指向前驱结点的引用 + } + } ``` === "TypeScript" diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index e9d02e7..23c1af9 100644 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -44,7 +44,7 @@ comments: true === "JavaScript" ```js title="list.js" - + var list = [1, 3, 2, 5, 4]; ``` === "TypeScript" @@ -107,7 +107,11 @@ comments: true === "JavaScript" ```js title="list.js" + /* 访问元素 */ + var num = list[1]; + /* 更新元素 */ + list[1] = 0; ``` === "TypeScript" @@ -203,7 +207,21 @@ comments: true === "JavaScript" ```js title="list.js" + /* 清空列表 */ + list = []; + /* 尾部添加元素 */ + list.push(1); + list.push(3); + list.push(2); + list.push(5); + list.push(4); + + /* 中间插入元素 */ + list.splice(3, 0, 6); + + /* 删除元素 */ + list.splice(3, 1); ``` === "TypeScript" @@ -295,7 +313,17 @@ comments: true === "JavaScript" ```js title="list.js" + /* 通过索引遍历列表 */ + var count = 0; + for (let i = 0; i < list.length; i++) { + count++; + } + /* 直接遍历列表元素 */ + count = 0; + for (const n of list) { + count++; + } ``` === "TypeScript" @@ -362,7 +390,9 @@ comments: true === "JavaScript" ```js title="list.js" - + /* 拼接两个列表 */ + var list1 = [ 6, 8, 7, 10, 9 ]; + list.push(...list1) ``` === "TypeScript" @@ -417,7 +447,8 @@ comments: true === "JavaScript" ```js title="list.js" - + /* 排序列表 */ + list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列 ``` === "TypeScript" @@ -713,7 +744,105 @@ comments: true === "JavaScript" ```js title="my_list.js" + /* 列表类简易实现 */ + class MyList { + nums; // 数组(存储列表元素) + _capacity = 10; // 列表容量 + _size = 0; // 列表长度(即当前元素数量) + extendRatio = 2; // 每次列表扩容的倍数 + /* 构造函数 */ + constructor() { + this.nums = new Array(this._capacity); + } + + /* 获取列表长度(即当前元素数量)*/ + get size() { + return this._size; + } + + /* 获取列表容量 */ + get capacity() { + return this._capacity; + } + + /* 访问元素 */ + get(index) { + // 索引如果越界则抛出异常,下同 + if (index >= this._size) + throw new Error("索引越界"); + return this.nums[index]; + } + + /* 更新元素 */ + set(index, num) { + if (index >= this._size) + throw new Error("索引越界"); + this.nums[index] = num; + } + + /* 尾部添加元素 */ + add(num) { + //元素数量超出容量时,触发扩容机制 + if (this._size === this._capacity) + this.extendCapacity(); + this.nums[this._size] = num; + // 更新元素数量 + this._size++; + } + + /* 中间插入元素 */ + insert(index, num) { + if (index >= this._size) + throw new Error("索引越界"); + // 元素数量超出容量时,触发扩容机制 + if (this._size === this._capacity) + this.extendCapacity(); + // 将索引 index 以及之后的元素都向后移动一位 + for (let j = this._size - 1; j >= index; j--) { + this.nums[j + 1] = this.nums[j]; + } + this.nums[index] = num; + // 更新元素数量 + this._size++; + } + + /* 删除元素 */ + remove(index) { + if (index >= this._size) + throw new Error("索引越界"); + let num = this.nums[index]; + // 将索引 index 之后的元素都向前移动一位 + for (let j = index; j < this._size - 1; j++) { + this.nums[j] = this.nums[j + 1]; + } + // 更新元素数量 + this._size--; + // 返回被删除元素 + return num; + } + + /* 列表扩容 */ + extendCapacity() { + // 新建一个长度为 size 的数组,并将原数组拷贝到新数组 + this.nums = this.nums.concat( + new Array(this.capacity * (this.extendRatio - 1)) + ); + // 更新列表容量 + this._capacity = this.nums.length; + } + + /* 将列表转换为数组 */ + toArray() { + let size = this.size; + // 仅转换有效长度范围内的列表元素 + let nums = new Array(size); + for (let i = 0; i < size; i++) { + nums[i] = this.get(i); + } + return nums; + } + } ``` === "TypeScript" From e862a06c4c1db5ce4ce14e654698ab356fa9ed01 Mon Sep 17 00:00:00 2001 From: machangxin Date: Mon, 12 Dec 2022 21:33:53 +0800 Subject: [PATCH 2/3] Quick sort using go --- .../chapter_sorting/quick_sort/quick_sort.go | 128 ++++++++++++++++++ .../quick_sort/quick_sort_test.go | 34 +++++ docs/chapter_sorting/quick_sort.md | 67 ++++++++- 3 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 codes/go/chapter_sorting/quick_sort/quick_sort.go create mode 100644 codes/go/chapter_sorting/quick_sort/quick_sort_test.go diff --git a/codes/go/chapter_sorting/quick_sort/quick_sort.go b/codes/go/chapter_sorting/quick_sort/quick_sort.go new file mode 100644 index 0000000..cc39bc1 --- /dev/null +++ b/codes/go/chapter_sorting/quick_sort/quick_sort.go @@ -0,0 +1,128 @@ +//File: quick_sort.go +//Created Time: 2022-12-12 +//Author: msk397 (machangxinq@gmail.com) + +package quick_sort + +// 快速排序 +type QuickSort struct{} + +// 快速排序(中位基准数优化) +type QuickSortMedian struct{} + +// 快速排序(尾递归优化) +type QuickSortTailCall struct{} + +/* 哨兵划分 */ +func (q *QuickSort) partition(nums []int, left, right int) int { + // 以 nums[left] 作为基准数 + i, j := left, right + for i < j { + for i < j && nums[j] >= nums[left] { + j-- // 从右向左找首个小于基准数的元素 + } + for i < j && nums[i] <= nums[left] { + i++ // 从左向右找首个大于基准数的元素 + } + // 元素交换 + nums[i], nums[j] = nums[j], nums[i] + } + // 将基准数交换至两子数组的分界线 + nums[i], nums[left] = nums[left], nums[i] + return i // 返回基准数的索引 +} + +/* 快速排序 */ +func (q *QuickSort) quickSort(nums []int, left, right int) { + // 子数组长度为 1 时终止递归 + if left >= right { + return + } + // 哨兵划分 + pivot := q.partition(nums, left, right) + // 递归左子数组、右子数组 + q.quickSort(nums, left, pivot-1) + q.quickSort(nums, pivot+1, right) +} + +/* 选取三个元素的中位数 */ +func (q *QuickSortMedian) medianThree(nums []int, left, mid, right int) int { + if (nums[left] > nums[mid]) != (nums[left] > nums[right]) { + return left + } else if (nums[mid] < nums[left]) != (nums[mid] > nums[right]) { + return mid + } + return right +} + +/* 哨兵划分(三数取中值)*/ +func (q *QuickSortMedian) partition(nums []int, left, right int) int { + // 以 nums[left] 作为基准数 + med := q.medianThree(nums, left, (left+right)/2, right) + // 将中位数交换至数组最左端 + nums[left], nums[med] = nums[med], nums[left] + // 以 nums[left] 作为基准数 + i, j := left, right + for i < j { + for i < j && nums[j] >= nums[left] { + j-- //从右向左找首个小于基准数的元素 + } + for i < j && nums[i] <= nums[left] { + i++ //从左向右找首个大于基准数的元素 + } + //元素交换 + nums[i], nums[j] = nums[j], nums[i] + } + //将基准数交换至两子数组的分界线 + nums[i], nums[left] = nums[left], nums[i] + return i //返回基准数的索引 +} + +/* 快速排序 */ +func (q *QuickSortMedian) quickSort(nums []int, left, right int) { + // 子数组长度为 1 时终止递归 + if left >= right { + return + } + // 哨兵划分 + pivot := q.partition(nums, left, right) + // 递归左子数组、右子数组 + q.quickSort(nums, left, pivot-1) + q.quickSort(nums, pivot+1, right) +} + +/* 哨兵划分 */ +func (q *QuickSortTailCall) partition(nums []int, left, right int) int { + // 以 nums[left] 作为基准数 + i, j := left, right + for i < j { + for i < j && nums[j] >= nums[left] { + j-- // 从右向左找首个小于基准数的元素 + } + for i < j && nums[i] <= nums[left] { + i++ // 从左向右找首个大于基准数的元素 + } + // 元素交换 + nums[i], nums[j] = nums[j], nums[i] + } + // 将基准数交换至两子数组的分界线 + nums[i], nums[left] = nums[left], nums[i] + return i // 返回基准数的索引 +} + +/* 快速排序(尾递归优化)*/ +func (q *QuickSortTailCall) quickSort(nums []int, left, right int) { + // 子数组长度为 1 时终止 + for left < right { + // 哨兵划分操作 + pivot := q.partition(nums, left, right) + // 对两个子数组中较短的那个执行快排 + if pivot-left < right-pivot { + q.quickSort(nums, left, pivot-1) // 递归排序左子数组 + left = pivot + 1 // 剩余待排序区间为 [pivot + 1, right] + } else { + q.quickSort(nums, pivot+1, right) // 递归排序右子数组 + right = pivot - 1 // 剩余待排序区间为 [left, pivot - 1] + } + } +} diff --git a/codes/go/chapter_sorting/quick_sort/quick_sort_test.go b/codes/go/chapter_sorting/quick_sort/quick_sort_test.go new file mode 100644 index 0000000..cd1925e --- /dev/null +++ b/codes/go/chapter_sorting/quick_sort/quick_sort_test.go @@ -0,0 +1,34 @@ +//File: quick_sort_test.go +//Created Time: 2022-12-12 +//Author: msk397 (machangxinq@gmail.com) + +package quick_sort + +import ( + "fmt" + "testing" +) + +// 快速排序 +func TestQuickSort(t *testing.T) { + q := QuickSort{} + nums := []int{4, 1, 3, 1, 5, 2} + q.quickSort(nums, 0, len(nums)-1) + fmt.Println("快速排序完成后 nums = ", nums) +} + +// 快速排序(中位基准数优化) +func TestQuickSortMedian(t *testing.T) { + q := QuickSortMedian{} + nums := []int{4, 1, 3, 1, 5, 2} + q.quickSort(nums, 0, len(nums)-1) + fmt.Println("快速排序(中位基准数优化)完成后 nums = ", nums) +} + +// 快速排序(尾递归优化) +func TestQuickSortTailCall(t *testing.T) { + q := QuickSortTailCall{} + nums := []int{4, 1, 3, 1, 5, 2} + q.quickSort(nums, 0, len(nums)-1) + fmt.Println("快速排序(尾递归优化)完成后 nums = ", nums) +} diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index 7934b59..d9ff7e1 100644 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -109,7 +109,24 @@ comments: true === "Go" ```go title="quick_sort.go" - + /* 哨兵划分 */ + func partition(nums []int, left, right int) int { + //以 nums[left] 作为基准数 + i, j := left, right + for i < j { + for i < j && nums[j] >= nums[left] { + j-- //从右向左找首个小于基准数的元素 + } + for i < j && nums[i] <= nums[left] { + i++ //从左向右找首个大于基准数的元素 + } + //元素交换 + nums[i], nums[j] = nums[j], nums[i] + } + //将基准数交换至两子数组的分界线 + nums[i], nums[left] = nums[left], nums[i] + return i //返回基准数的索引 + } ``` === "JavaScript" @@ -225,7 +242,18 @@ comments: true === "Go" ```go title="quick_sort.go" - + /* 快速排序 */ + func quickSort(nums []int, left, right int) { + // 子数组长度为 1 时终止递归 + if left >= right { + return + } + // 哨兵划分 + pivot := partition(nums, left, right) + // 递归左子数组、右子数组 + quickSort(nums, left, pivot-1) + quickSort(nums, pivot+1, right) + } ``` === "JavaScript" @@ -369,7 +397,25 @@ comments: true === "Go" ```go title="quick_sort.go" + /* 选取三个元素的中位数 */ + func medianThree(nums []int, left, mid, right int) int { + if (nums[left] > nums[mid]) != (nums[left] > nums[right]) { + return left + } else if (nums[mid] < nums[left]) != (nums[mid] > nums[right]) { + return mid + } + return right + } + /* 哨兵划分(三数取中值)*/ + func partition(nums []int, left, right int) int { + // 以 nums[left] 作为基准数 + med := medianThree(nums, left, (left+right)/2, right) + // 将中位数交换至数组最左端 + nums[left], nums[med] = nums[med], nums[left] + // 以 nums[left] 作为基准数 + // 下同省略... + } ``` === "JavaScript" @@ -485,7 +531,22 @@ comments: true === "Go" ```go title="quick_sort.go" - + /* 快速排序(尾递归优化)*/ + func quickSort(nums []int, left, right int) { + // 子数组长度为 1 时终止 + for left < right { + // 哨兵划分操作 + pivot := partition(nums, left, right) + // 对两个子数组中较短的那个执行快排 + if pivot-left < right-pivot { + quickSort(nums, left, pivot-1) // 递归排序左子数组 + left = pivot + 1 // 剩余待排序区间为 [pivot + 1, right] + } else { + quickSort(nums, pivot+1, right) // 递归排序右子数组 + right = pivot - 1 // 剩余待排序区间为 [left, pivot - 1] + } + } + } ``` === "JavaScript" From dc3ac1fae66a9aea15e1a10565f0e9b1d841c3a7 Mon Sep 17 00:00:00 2001 From: IsChristina <315644507@qq.com> Date: Mon, 12 Dec 2022 21:39:48 +0800 Subject: [PATCH 3/3] add ; --- docs/chapter_array_and_linkedlist/array.md | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index c6f2fec..917c75a 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -50,8 +50,8 @@ comments: true ```javascript title="array.js" /* 初始化数组 */ - var arr = new Array(5).fill(0) - var nums = [1, 3, 2, 5, 4] + var arr = new Array(5).fill(0); + var nums = [1, 3, 2, 5, 4]; ``` === "TypeScript" @@ -140,10 +140,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 随机返回一个数组元素 */ function randomAccess(nums){ // 在区间 [0, nums.length) 中随机抽取一个数字 - const random_index = Math.floor(Math.random() * nums.length) + const random_index = Math.floor(Math.random() * nums.length); // 获取并返回随机元素 - random_num = nums[random_index] - return random_num + random_num = nums[random_index]; + return random_num; } ``` @@ -236,13 +236,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 扩展数组长度 */ function extend(nums, enlarge){ // 初始化一个扩展长度后的数组 - let res = new Array(nums.length + enlarge).fill(0) + let res = new Array(nums.length + enlarge).fill(0); // 将原数组中的所有元素复制到新数组 for(let i=0; i