build
This commit is contained in:
parent
61ae2b50ba
commit
b9848fc87c
@ -151,13 +151,48 @@ comments: true
|
|||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="binary_search_edge.js"
|
```javascript title="binary_search_edge.js"
|
||||||
[class]{}-[func]{binarySearchLeftEdge}
|
/* 二分查找最左一个元素 */
|
||||||
|
function binarySearchLeftEdge(nums, target) {
|
||||||
|
let i = 0,
|
||||||
|
j = nums.length - 1; // 初始化双闭区间 [0, n-1]
|
||||||
|
while (i <= j) {
|
||||||
|
let m = Math.floor((i + j) / 2); // 计算中点索引 m
|
||||||
|
if (nums[m] < target) {
|
||||||
|
i = m + 1; // target 在区间 [m+1, j] 中
|
||||||
|
} else if (nums[m] > target) {
|
||||||
|
j = m - 1; // target 在区间 [i, m-1] 中
|
||||||
|
} else {
|
||||||
|
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == nums.length || nums[i] != target) {
|
||||||
|
return -1; // 未找到目标元素,返回 -1
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="binary_search_edge.ts"
|
```typescript title="binary_search_edge.ts"
|
||||||
[class]{}-[func]{binarySearchLeftEdge}
|
/* 二分查找最左一个元素 */
|
||||||
|
function binarySearchLeftEdge(nums: number[], target: number): number {
|
||||||
|
let i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
|
||||||
|
while (i <= j) {
|
||||||
|
let m = Math.floor((i + j) / 2); // 计算中点索引 m
|
||||||
|
if (nums[m] < target) {
|
||||||
|
i = m + 1; // target 在区间 [m+1, j] 中
|
||||||
|
} else if (nums[m] > target) {
|
||||||
|
j = m - 1; // target 在区间 [i, m-1] 中
|
||||||
|
} else {
|
||||||
|
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == nums.length || nums[i] != target) {
|
||||||
|
return -1; // 未找到目标元素,返回 -1
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
@ -352,13 +387,48 @@ comments: true
|
|||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="binary_search_edge.js"
|
```javascript title="binary_search_edge.js"
|
||||||
[class]{}-[func]{binarySearchRightEdge}
|
/* 二分查找最右一个元素 */
|
||||||
|
function binarySearchRightEdge(nums, target) {
|
||||||
|
let i = 0,
|
||||||
|
j = nums.length - 1; // 初始化双闭区间 [0, n-1]
|
||||||
|
while (i <= j) {
|
||||||
|
let m = Math.floor((i + j) / 2); // 计算中点索引 m
|
||||||
|
if (nums[m] < target) {
|
||||||
|
i = m + 1; // target 在区间 [m+1, j] 中
|
||||||
|
} else if (nums[m] > target) {
|
||||||
|
j = m - 1; // target 在区间 [i, m-1] 中
|
||||||
|
} else {
|
||||||
|
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j == nums.length || nums[j] != target) {
|
||||||
|
return -1; // 未找到目标元素,返回 -1
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="binary_search_edge.ts"
|
```typescript title="binary_search_edge.ts"
|
||||||
[class]{}-[func]{binarySearchRightEdge}
|
/* 二分查找最右一个元素 */
|
||||||
|
function binarySearchRightEdge(nums: number[], target: number): number {
|
||||||
|
let i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
|
||||||
|
while (i <= j) {
|
||||||
|
let m = Math.floor((i + j) / 2); // 计算中点索引 m
|
||||||
|
if (nums[m] < target) {
|
||||||
|
i = m + 1; // target 在区间 [m+1, j] 中
|
||||||
|
} else if (nums[m] > target) {
|
||||||
|
j = m - 1; // target 在区间 [i, m-1] 中
|
||||||
|
} else {
|
||||||
|
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j == nums.length || nums[j] != target) {
|
||||||
|
return -1; // 未找到目标元素,返回 -1
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
@ -230,17 +230,87 @@ comments: true
|
|||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="heap_sort.js"
|
```javascript title="heap_sort.js"
|
||||||
[class]{}-[func]{siftDown}
|
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||||
|
function siftDown(nums, n, i) {
|
||||||
|
while (true) {
|
||||||
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||||
|
let l = 2 * i + 1;
|
||||||
|
let r = 2 * i + 2;
|
||||||
|
let ma = i;
|
||||||
|
if (l < n && nums[l] > nums[ma]) {
|
||||||
|
ma = l;
|
||||||
|
}
|
||||||
|
if (r < n && nums[r] > nums[ma]) {
|
||||||
|
ma = r;
|
||||||
|
}
|
||||||
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||||
|
if (ma === i) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 交换两节点
|
||||||
|
[nums[i], nums[ma]] = [nums[ma], nums[i]];
|
||||||
|
// 循环向下堆化
|
||||||
|
i = ma;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[class]{}-[func]{heapSort}
|
/* 堆排序 */
|
||||||
|
function heapSort(nums) {
|
||||||
|
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||||
|
for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {
|
||||||
|
siftDown(nums, nums.length, i);
|
||||||
|
}
|
||||||
|
// 从堆中提取最大元素,循环 n-1 轮
|
||||||
|
for (let i = nums.length - 1; i > 0; i--) {
|
||||||
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||||
|
[nums[0], nums[i]] = [nums[i], nums[0]];
|
||||||
|
// 以根节点为起点,从顶至底进行堆化
|
||||||
|
siftDown(nums, i, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="heap_sort.ts"
|
```typescript title="heap_sort.ts"
|
||||||
[class]{}-[func]{siftDown}
|
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||||
|
function siftDown(nums: number[], n: number, i: number): void {
|
||||||
|
while (true) {
|
||||||
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||||
|
let l = 2 * i + 1;
|
||||||
|
let r = 2 * i + 2;
|
||||||
|
let ma = i;
|
||||||
|
if (l < n && nums[l] > nums[ma]) {
|
||||||
|
ma = l;
|
||||||
|
}
|
||||||
|
if (r < n && nums[r] > nums[ma]) {
|
||||||
|
ma = r;
|
||||||
|
}
|
||||||
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||||
|
if (ma === i) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 交换两节点
|
||||||
|
[nums[i], nums[ma]] = [nums[ma], nums[i]];
|
||||||
|
// 循环向下堆化
|
||||||
|
i = ma;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[class]{}-[func]{heapSort}
|
/* 堆排序 */
|
||||||
|
function heapSort(nums: number[]): void {
|
||||||
|
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||||
|
for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {
|
||||||
|
siftDown(nums, nums.length, i);
|
||||||
|
}
|
||||||
|
// 从堆中提取最大元素,循环 n-1 轮
|
||||||
|
for (let i = nums.length - 1; i > 0; i--) {
|
||||||
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||||
|
[nums[0], nums[i]] = [nums[i], nums[0]];
|
||||||
|
// 以根节点为起点,从顶至底进行堆化
|
||||||
|
siftDown(nums, i, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
@ -134,13 +134,43 @@ comments: true
|
|||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="selection_sort.js"
|
```javascript title="selection_sort.js"
|
||||||
[class]{}-[func]{selectionSort}
|
/* 选择排序 */
|
||||||
|
function selectionSort(nums) {
|
||||||
|
let n = nums.length;
|
||||||
|
// 外循环:未排序区间为 [i, n-1]
|
||||||
|
for (let i = 0; i < n - 1; i++) {
|
||||||
|
// 内循环:找到未排序区间内的最小元素
|
||||||
|
let k = i;
|
||||||
|
for (let j = i + 1; j < n; j++) {
|
||||||
|
if (nums[j] < nums[k]) {
|
||||||
|
k = j; // 记录最小元素的索引
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将该最小元素与未排序区间的首个元素交换
|
||||||
|
[nums[i], nums[k]] = [nums[k], nums[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="selection_sort.ts"
|
```typescript title="selection_sort.ts"
|
||||||
[class]{}-[func]{selectionSort}
|
/* 选择排序 */
|
||||||
|
function selectionSort(nums: number[]): void {
|
||||||
|
let n = nums.length;
|
||||||
|
// 外循环:未排序区间为 [i, n-1]
|
||||||
|
for (let i = 0; i < n - 1; i++) {
|
||||||
|
// 内循环:找到未排序区间内的最小元素
|
||||||
|
let k = i;
|
||||||
|
for (let j = i + 1; j < n; j++) {
|
||||||
|
if (nums[j] < nums[k]) {
|
||||||
|
k = j; // 记录最小元素的索引
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将该最小元素与未排序区间的首个元素交换
|
||||||
|
[nums[i], nums[k]] = [nums[k], nums[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
Loading…
Reference in New Issue
Block a user