1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-07-11 01:26:42 +08:00

feat: 修复文档,新增算法代码

This commit is contained in:
妹妹下雨回不去
2023-03-02 17:39:42 +08:00
parent 7bd3072ee1
commit 8262d7ab42
58 changed files with 1725 additions and 1679 deletions

View File

@@ -0,0 +1,60 @@
/*
* @Description: 折半插入排序【JavaScript版本】
* @Version: Beta1.0
* @Author: 【B站&公众号】Rong姐姐好可爱
* @Date: 2021-03-27 12:35:17
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
* @LastEditTime: 2021-03-27 12:50:00
*/
/**
* 折半插入排序
* @param {Array} arr 待排序数组
* @param {int} len 数组长度
*/
function binaryInsertSort(arr,len){
// 数组长度校验【非必须】
len=arr.length===len?len:arr.length
for(let i=1;i<len;i++){
const temp=arr[i];
let lowIndex=0,highIndex=i-1;
while(lowIndex<=highIndex){
// 注意:取整,javascript这里取整会出现空指针
const mid=parseInt((lowIndex+highIndex)/2);
if(arr[mid]<=temp){
// 右侧
lowIndex=mid+1
}else{
// 左侧
highIndex=mid-1
}
}
// 元素后移
for(let j=i-1;j>highIndex;--j){
arr[j+1]=arr[j]
}
arr[highIndex+1]=temp;
}
// 返回经过折半插入排序处理的有序数组
return arr;
}
const dealArr=[5,2,7,3,18,8,12,1]
console.log('插入排序前:',dealArr)
const sortResult=binaryInsertSort(dealArr,7)
console.log('插入排序后:',sortResult)