1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-05-04 21:50:28 +08:00

refactor: eslint规则,删除无用算法代码

This commit is contained in:
妹妹下雨回不去
2023-03-02 22:26:28 +08:00
parent c3cbc0353a
commit 6995f595a2
122 changed files with 327 additions and 5986 deletions

View File

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

View File

@@ -8,23 +8,21 @@
*/
function BubbleSort (arr, len) {
function BubbleSort(arr, len) {
// 校正数组的长度
len = arr.length == len ? len : arr.length
len = arr.length === len ? len : arr.length
// 冒泡排序让数组arr有序
for (let i = 0; i < len - 1; i++) {
let isSorted = false;
let isSorted = false
// len个数组进行len-1趟一趟冒泡
for (let j = len - 1; j > i; j--) {
// 注意这里的for循环倒序是有讲究的想象一下泡泡不都是网上升的么....
if (arr[j - 1] > arr[j]) {
// 交换元素,始终让最小的元素往上走(冒泡)
const temp = arr[j - 1];
arr[j - 1] = arr[j];
const temp = arr[j - 1]
arr[j - 1] = arr[j]
arr[j] = temp
// 需要冒泡
@@ -33,7 +31,7 @@ function BubbleSort (arr, len) {
}
// 第一趟比较后,如果本身序列是有序的,就直接跳出循环
if (isSorted === false) {
break;
break
}
}
@@ -42,20 +40,19 @@ function BubbleSort (arr, len) {
/**
*
* 加减法交换元素的值
* 注意JavaScript中使用需要考虑到作用域的问题
* @param {int} a
* @param {int} b
* @param a
* @param b
*/
function swap (a, b) {
a = a + b;
b = a - b;
a = a - b;
function swap(a, b) {
a = a + b
b = a - b
a = a - b
}
const initArr = [1, 5, 8, 3, 2, 9, 16]
console.log(`冒泡排序前:${initArr}`)
const sortedArr = BubbleSort(initArr, 7);
console.log(`冒泡排序后:${sortedArr}`)
const sortedArr = BubbleSort(initArr, 7)
console.log(`冒泡排序后:${sortedArr}`)

View File

@@ -10,61 +10,58 @@
/**
* 基于分治法思想,将数组进行快速排序
* @param {Array} arr 待排序的数组
* @param {Array} arr 待排序的数组
* @param {int} low 数组低位角标 左指针
* @param {int} high 数组高位角标 右指针
* @returns
* @returns
*/
function QuickSort(arr,low,high){
function QuickSort(arr, low, high) {
// low=high 说明只有一个元素,理解为有序,不做处理
// low>high 说明左右指针已经重合,数组已经遍历完,需要跳出
if(low<high){
let pivotIndex=Partition(arr,low,high);
if (low < high) {
const pivotIndex = Partition(arr, low, high)
// 处理左侧
QuickSort(arr,low,pivotIndex-1);
QuickSort(arr, low, pivotIndex - 1)
// 处理右侧
QuickSort(arr,pivotIndex+1,high)
QuickSort(arr, pivotIndex + 1, high)
}
// 经过快排处理的数组
return arr;
return arr
}
/**
*
*
* 寻找数组中的基准pivot使得左侧元素全部小于等于pivot右侧元素全部大于等于pivot
* @param {Array} arr 分治思想处理后的数组
* @param {Array} arr 分治思想处理后的数组
* @param {int} low 数组低位角标 左指针
* @param {int} high 数组高位角标 右指针
* @returns
* @returns
*/
function Partition(arr,low,high){
function Partition(arr, low, high) {
// 假设低位指针对应数组角标元素为基准pivot
const pivot=arr[low];
while(low<high){
const pivot = arr[low]
while (low < high) {
// 从右往左直到比pivot小跳出循环
while(low<high && arr[high]>= pivot ) --high;
arr[low]=arr[high]
while (low < high && arr[high] >= pivot) --high
arr[low] = arr[high]
// 从左往右直到比pivot大跳出循环
while(low<high && arr[low]<= pivot) ++low;
arr[high]=arr[low]
while (low < high && arr[low] <= pivot) ++low
arr[high] = arr[low]
}
// 基准值移到最终的位置此时左侧小于等于pivot 右侧大于等于pivot
arr[low]=pivot
arr[low] = pivot
// 返回基准值的角标
return low;
return low
}
const initArr=[2,18,6,25,19,4,8,3,7];
const initArr = [2, 18, 6, 25, 19, 4, 8, 3, 7]
console.log(`快速排序处理前:${initArr}`)
const quickSortResult=QuickSort(initArr,0,8)
const quickSortResult = QuickSort(initArr, 0, 8)
console.log(`快速排序处理后:${quickSortResult}`)

View File

@@ -8,16 +8,14 @@
*/
/**
*
*
* 数组的希尔排序
* @param {Array} arr 待排序数组
* @param {Array} arr 待排序数组
* @param {int} len 数组长度,可校验
* @returns 返回已排序的数组,从小到大
* @returns 返回已排序的数组,从小到大
*/
function shellSort (arr, len) {
function shellSort(arr, len) {
// 校对数组长度
len = arr.length === len ? len : arr.length
@@ -25,10 +23,8 @@ function shellSort (arr, len) {
for (let increment = Math.floor(len / 2); increment >= 1; increment = Math.floor(increment / 2)) {
// 对每组数据,进行直接排序
for (let groupIndex = 0; groupIndex < increment; ++groupIndex) {
specialStraightInsertSort(arr, len, increment, groupIndex)
}
}
return arr
@@ -36,30 +32,30 @@ function shellSort (arr, len) {
/**
* 根据希尔排序的步长对分组进行直接插入排序处理
* @param {Array} arr 排序数组
* @param {Array} arr 排序数组
* @param {int} len 数组长度
* @param {int} increment 增量步长
* @param {int} groupIndex 分组,第几个分组
*/
function specialStraightInsertSort (arr, len, increment, groupIndex) {
function specialStraightInsertSort(arr, len, increment, groupIndex) {
len = arr.length === len ? len : arr.length
console.log(`数组长度:${len}----->当前步长:${increment}---->分组:${groupIndex}`)
for(let eleStartIndex=groupIndex+increment;eleStartIndex<len;eleStartIndex+=increment){
for (let eleStartIndex = groupIndex + increment; eleStartIndex < len; eleStartIndex += increment) {
// 此时eleStartIndex为直接插入排序的比较元素
// 直接插入排序中的哨兵元素【重要】
const temp=arr[eleStartIndex]
let j;
const temp = arr[eleStartIndex]
let j
// 向前比较 从小到大
for(j=eleStartIndex-increment;j>=0&&arr[j]>temp;j-=increment){
arr[j+increment]=arr[j]
for (j = eleStartIndex - increment; j >= 0 && arr[j] > temp; j -= increment) {
arr[j + increment] = arr[j]
}
arr[j+increment]=temp
arr[j + increment] = temp
}
console.log('specialStraightInsertSort处理后',arr)
console.log('specialStraightInsertSort处理后', arr)
return arr
}
@@ -69,18 +65,17 @@ function specialStraightInsertSort (arr, len, increment, groupIndex) {
* @param{Array} arr 待排序的数组
* @param{int} len 数组arr的长度可以用arr.length()计算得到
*/
function straightInsertSort (arr, len) {
function straightInsertSort(arr, len) {
// 重新确定数组长度
len = arr.length === len ? len : arr.length;
len = arr.length === len ? len : arr.length
// 从第二个元素开始循环共len-1次
for (let i = 1; i < len; i++) {
// 后面的额元素比前面的元素小,需要把前面大于哨兵元素有序序列,移动后面一位
if (arr[i] < arr[i - 1]) {
let j;
let j
// 哨兵元素
const temp = arr[i];
const temp = arr[i]
for (j = i - 1; arr[j] > temp; --j) {
// 后移
arr[j + 1] = arr[j]
@@ -93,7 +88,6 @@ function straightInsertSort (arr, len) {
}
return arr
}
@@ -105,26 +99,26 @@ console.log('插入排序后:', sortResult)
/**
* 简化的希尔排序
* @param {Array} arr
* @param {Array} arr
* @returns 返回已排序号的数组,从小到大
*/
function shellSortBetter (arr) {
var len = arr.length;
var increment = Math.floor(len / 2);
while (increment != 0) {
for (var i = increment; i < len; i++) {
var temp = arr[i]
function shellSortBetter(arr) {
const len = arr.length
let increment = Math.floor(len / 2)
while (increment !== 0) {
for (let i = increment; i < len; i++) {
const temp = arr[i]
for (var j = i - increment; j >= 0 && temp < arr[j]; j -= increment) {
arr[j + increment] = arr[j]
}
arr[j + increment] = temp;
arr[j + increment] = temp
}
increment = Math.floor(increment / 2)
}
return arr;
return arr
}
console.log('简化shellSortBetter希尔排序前', dealArr)
const sortResultBetter = shellSortBetter(dealArr)
console.log('简化shellSortBetter希尔排序后', sortResultBetter)
console.log('简化shellSortBetter希尔排序后', sortResultBetter)

View File

@@ -13,37 +13,35 @@
* @param{Array} arr 待排序的数组
* @param{int} len 数组arr的长度可以用arr.length()计算得到
*/
function straightInsertSort(arr,len){
function straightInsertSort(arr, len) {
// 重新确定数组长度
len=arr.length===len?len:arr.length;
len = arr.length === len ? len : arr.length
// 从第二个元素开始循环共len-1次
for(let i=1;i<len;i++){
for (let i = 1; i < len; i++) {
// 后面的额元素比前面的元素小,需要把前面大于哨兵元素有序序列,移动后面一位
if(arr[i]<arr[i-1]){
let j;
if (arr[i] < arr[i - 1]) {
let j
// 哨兵元素
const temp=arr[i];
for(j=i-1; arr[j]>temp;--j){
// 后移
arr[j+1]=arr[j]
const temp = arr[i]
for (j = i - 1; arr[j] > temp; --j) {
// 后移
arr[j + 1] = arr[j]
}
// 跳出循环逻辑出现arr[j] > arr[j-1]
// 哨兵即待排序的
arr[j+1]=temp
arr[j + 1] = temp
}
}
return arr
}
const dealArr=[5,2,7,3,18,8,12,1]
console.log('插入排序前:',dealArr)
const sortResult=straightInsertSort(dealArr,7)
const dealArr = [5, 2, 7, 3, 18, 8, 12, 1]
console.log('插入排序前:', dealArr)
const sortResult = straightInsertSort(dealArr, 7)
console.log('插入排序后:',sortResult)
console.log('插入排序后:', sortResult)