mirror of
https://github.com/142vip/408CSFamily.git
synced 2026-02-03 02:23:38 +08:00
Merge pull request #119 from 142vip/feat/ts-style
feat: 优化`ts`版本算法代码,删除冗余算法代码,基于`Eslint`格式化
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* 折半插入排序【JavaScript版本】
|
||||
*/
|
||||
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
|
||||
let highIndex = i - 1
|
||||
|
||||
while (lowIndex <= highIndex) {
|
||||
// 注意:取整,javascript这里取整,会出现空指针
|
||||
const mid = Math.ceil((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)
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* 冒泡排序【JavaScript版本】
|
||||
*/
|
||||
function BubbleSort(arr, len) {
|
||||
// 校正数组的长度
|
||||
len = arr.length === len ? len : arr.length
|
||||
|
||||
// 冒泡排序,让数组arr有序
|
||||
for (let i = 0; i < len - 1; i++) {
|
||||
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]
|
||||
arr[j] = temp
|
||||
|
||||
// 需要冒泡
|
||||
isSorted = true
|
||||
}
|
||||
}
|
||||
// 第一趟比较后,如果本身序列是有序的,就直接跳出循环
|
||||
if (isSorted === false) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
const initArr = [1, 5, 8, 3, 2, 9, 16]
|
||||
console.log(`冒泡排序前:${initArr}`)
|
||||
const sortedArr = BubbleSort(initArr, 7)
|
||||
console.log(`冒泡排序后:${sortedArr}`)
|
||||
@@ -1,52 +0,0 @@
|
||||
/**
|
||||
* 基于分治法思想,将数组进行快速排序
|
||||
* @param {Array} arr 待排序的数组
|
||||
* @param {int} low 数组低位角标 左指针
|
||||
* @param {int} high 数组高位角标 右指针
|
||||
*/
|
||||
function QuickSort(arr, low, high) {
|
||||
// low=high 说明只有一个元素,理解为有序,不做处理
|
||||
// low>high 说明左、右指针已经重合,数组已经遍历完,需要跳出
|
||||
if (low < high) {
|
||||
const pivotIndex = Partition(arr, low, high)
|
||||
// 处理左侧
|
||||
QuickSort(arr, low, pivotIndex - 1)
|
||||
// 处理右侧
|
||||
QuickSort(arr, pivotIndex + 1, high)
|
||||
}
|
||||
|
||||
// 经过快排处理的数组
|
||||
return arr
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 寻找数组中的基准pivot,使得左侧元素全部小于等于pivot,右侧元素全部大于等于pivot
|
||||
* @param {Array} arr 分治思想处理后的数组
|
||||
* @param {int} low 数组低位角标 左指针
|
||||
* @param {int} high 数组高位角标 右指针
|
||||
*/
|
||||
function Partition(arr, low, high) {
|
||||
// 假设低位指针对应数组角标元素为基准pivot
|
||||
const pivot = arr[low]
|
||||
while (low < high) {
|
||||
// 从右往左直到比pivot小跳出循环
|
||||
while (low < high && arr[high] >= pivot) --high
|
||||
arr[low] = arr[high]
|
||||
|
||||
// 从左往右直到比pivot大跳出循环
|
||||
while (low < high && arr[low] <= pivot) ++low
|
||||
arr[high] = arr[low]
|
||||
}
|
||||
|
||||
// 基准值移到最终的位置,此时左侧小于等于pivot 右侧大于等于pivot
|
||||
arr[low] = pivot
|
||||
|
||||
// 返回基准值的角标
|
||||
return low
|
||||
}
|
||||
|
||||
const initArr = [2, 18, 6, 25, 19, 4, 8, 3, 7]
|
||||
console.log(`快速排序处理前:${initArr}`)
|
||||
const quickSortResult = QuickSort(initArr, 0, 8)
|
||||
console.log(`快速排序处理后:${quickSortResult}`)
|
||||
@@ -1,79 +0,0 @@
|
||||
/**
|
||||
* 数组的希尔排序
|
||||
* - 返回已排序的数组,从小到大
|
||||
* @param {Array} arr 待排序数组
|
||||
* @param {int} len 数组长度,可校验
|
||||
* @returns
|
||||
*/
|
||||
function shellSort(arr, len) {
|
||||
// 校对数组长度
|
||||
len = arr.length === len ? len : arr.length
|
||||
|
||||
// 注意处理浮点【向上取整】 防止空指针
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据希尔排序的步长对分组进行直接插入排序处理
|
||||
* @param {Array} arr 排序数组
|
||||
* @param {int} len 数组长度
|
||||
* @param {int} increment 增量步长
|
||||
* @param {int} 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) {
|
||||
// 此时eleStartIndex为直接插入排序的比较元素
|
||||
|
||||
// 直接插入排序中的哨兵元素【重要】
|
||||
const temp = arr[eleStartIndex]
|
||||
let j
|
||||
// 向前比较 从小到大
|
||||
for (j = eleStartIndex - increment; j >= 0 && arr[j] > temp; j -= increment) {
|
||||
arr[j + increment] = arr[j]
|
||||
}
|
||||
arr[j + increment] = temp
|
||||
}
|
||||
|
||||
console.log('specialStraightInsertSort处理后:', arr)
|
||||
return arr
|
||||
}
|
||||
|
||||
const dealArr = [5, 8, 2, 16, 3, 9, 1]
|
||||
console.log('插入排序前:', dealArr)
|
||||
const sortResult = shellSort(dealArr, 7)
|
||||
console.log('插入排序后:', sortResult)
|
||||
|
||||
/**
|
||||
* 简化的希尔排序
|
||||
* - 返回已排序号的数组,从小到大
|
||||
* @param {Array} arr
|
||||
*/
|
||||
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 (let j = i - increment; j >= 0 && temp < arr[j]; j -= increment) {
|
||||
arr[j + increment] = arr[j]
|
||||
}
|
||||
arr[i + increment] = temp
|
||||
}
|
||||
increment = Math.floor(increment / 2)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
console.log('简化shellSortBetter希尔排序前:', dealArr)
|
||||
const sortResultBetter = shellSortBetter(dealArr)
|
||||
console.log('简化shellSortBetter希尔排序后:', sortResultBetter)
|
||||
@@ -1,32 +0,0 @@
|
||||
/**
|
||||
* 直接插入排序【JavaScript版本】
|
||||
*/
|
||||
function straightInsertSort(arr, len) {
|
||||
// 重新确定数组长度
|
||||
len = arr.length === len ? len : arr.length
|
||||
|
||||
// 从第二个元素开始循环,共len-1次
|
||||
for (let i = 1; i < len; i++) {
|
||||
// 后面的额元素比前面的元素小,需要把前面大于哨兵元素有序序列,移动后面一位
|
||||
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]
|
||||
}
|
||||
// 跳出循环逻辑,出现arr[j] > arr[j-1]
|
||||
// 哨兵即待排序的
|
||||
arr[j + 1] = temp
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
const dealArr = [5, 2, 7, 3, 18, 8, 12, 1]
|
||||
console.log('插入排序前:', dealArr)
|
||||
const sortResult = straightInsertSort(dealArr, 7)
|
||||
|
||||
console.log('插入排序后:', sortResult)
|
||||
@@ -1,11 +1,11 @@
|
||||
/**
|
||||
* 折半插入排序【JavaScript版本】
|
||||
* 折半插入排序
|
||||
*/
|
||||
function binaryInsertSort(arr, len) {
|
||||
function binaryInsertSort(arr: number[], len: number): number[] {
|
||||
// 数组长度校验【非必须】
|
||||
len = arr.length === len
|
||||
? len
|
||||
: arr.length
|
||||
if (arr.length !== len) {
|
||||
len = arr.length
|
||||
}
|
||||
|
||||
// 遍历
|
||||
for (let i = 1; i < len; i++) {
|
||||
@@ -30,14 +30,17 @@ function binaryInsertSort(arr, len) {
|
||||
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)
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
interface SwitchValue {
|
||||
a: number
|
||||
b: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序算法:冒泡排序
|
||||
* 给定一个数组,按照从小到大或从大到小排序,打印排序前后结果对比
|
||||
* 编程语言:TypeScript
|
||||
*/
|
||||
function BubbleSort(arr: Array<number>): number[] {
|
||||
export function BubbleSort(arr: number[]): number[] {
|
||||
// 获取数组长度
|
||||
const len = arr.length
|
||||
|
||||
@@ -25,7 +29,7 @@ function BubbleSort(arr: Array<number>): number[] {
|
||||
}
|
||||
}
|
||||
// 第一趟比较后,如果本身序列是有序的,就直接跳出循环
|
||||
if (isSorted === false) {
|
||||
if (!isSorted) {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -33,18 +37,15 @@ function BubbleSort(arr: Array<number>): number[] {
|
||||
return arr
|
||||
}
|
||||
|
||||
interface SwitchValue {
|
||||
a: number
|
||||
b: number
|
||||
}
|
||||
/**
|
||||
* 将两个变量数值交换
|
||||
*/
|
||||
function _switchValue(params: SwitchValue) {
|
||||
export function switchValue(params: SwitchValue) {
|
||||
const { a: newB, b: newA } = params
|
||||
return { a: newA, b: newB }
|
||||
}
|
||||
|
||||
// 用例
|
||||
const initArr = [1, 5, 8, 3, 2, 9, 16]
|
||||
console.log(`冒泡排序前:${initArr}`)
|
||||
const sortedArr = BubbleSort(initArr)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* @param {int} low 数组低位角标 左指针
|
||||
* @param {int} high 数组高位角标 右指针
|
||||
*/
|
||||
function QuickSort(arr, low, high) {
|
||||
export function QuickSort(arr, low, high) {
|
||||
// low=high 说明只有一个元素,理解为有序,不做处理
|
||||
// low>high 说明左、右指针已经重合,数组已经遍历完,需要跳出
|
||||
if (low < high) {
|
||||
@@ -26,7 +26,7 @@ function QuickSort(arr, low, high) {
|
||||
* @param {int} low 数组低位角标 左指针
|
||||
* @param {int} high 数组高位角标 右指针
|
||||
*/
|
||||
function Partition(arr, low, high) {
|
||||
export function Partition(arr, low, high) {
|
||||
// 假设低位指针对应数组角标元素为基准pivot
|
||||
const pivot = arr[low]
|
||||
while (low < high) {
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
* - 返回已排序的数组,从小到大
|
||||
* @param {Array} arr 待排序数组
|
||||
* @param {int} len 数组长度,可校验
|
||||
* @returns
|
||||
*/
|
||||
function shellSort(arr, len) {
|
||||
export function shellSort(arr: number[], len: number) {
|
||||
// 校对数组长度
|
||||
len = arr.length === len ? len : arr.length
|
||||
if (arr.length !== len) {
|
||||
len = arr.length
|
||||
}
|
||||
|
||||
// 注意处理浮点【向上取整】 防止空指针
|
||||
for (let increment = Math.floor(len / 2); increment >= 1; increment = Math.floor(increment / 2)) {
|
||||
@@ -27,8 +28,11 @@ function shellSort(arr, len) {
|
||||
* @param {int} increment 增量步长
|
||||
* @param {int} groupIndex 分组,第几个分组
|
||||
*/
|
||||
function specialStraightInsertSort(arr, len, increment, groupIndex) {
|
||||
len = arr.length === len ? len : arr.length
|
||||
export function specialStraightInsertSort(arr: number[], len: number, increment: number, groupIndex: number) {
|
||||
if (arr.length !== len) {
|
||||
len = arr.length
|
||||
}
|
||||
|
||||
console.log(`数组长度:${len}----->当前步长:${increment}---->分组:${groupIndex}`)
|
||||
|
||||
for (let eleStartIndex = groupIndex + increment; eleStartIndex < len; eleStartIndex += increment) {
|
||||
@@ -36,7 +40,8 @@ function specialStraightInsertSort(arr, len, increment, groupIndex) {
|
||||
|
||||
// 直接插入排序中的哨兵元素【重要】
|
||||
const temp = arr[eleStartIndex]
|
||||
let j
|
||||
|
||||
let j: number
|
||||
// 向前比较 从小到大
|
||||
for (j = eleStartIndex - increment; j >= 0 && arr[j] > temp; j -= increment) {
|
||||
arr[j + increment] = arr[j]
|
||||
@@ -45,6 +50,7 @@ function specialStraightInsertSort(arr, len, increment, groupIndex) {
|
||||
}
|
||||
|
||||
console.log('specialStraightInsertSort处理后:', arr)
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
@@ -58,9 +64,10 @@ console.log('插入排序后:', sortResult)
|
||||
* - 返回已排序号的数组,从小到大
|
||||
* @param {Array} arr
|
||||
*/
|
||||
function shellSortBetter(arr) {
|
||||
export 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]
|
||||
@@ -74,6 +81,7 @@ function shellSortBetter(arr) {
|
||||
return arr
|
||||
}
|
||||
|
||||
// 测试用例
|
||||
console.log('简化shellSortBetter希尔排序前:', dealArr)
|
||||
const sortResultBetter = shellSortBetter(dealArr)
|
||||
console.log('简化shellSortBetter希尔排序后:', sortResultBetter)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
/**
|
||||
* 直接插入排序【JavaScript版本】
|
||||
*/
|
||||
function straightInsertSort(arr, len) {
|
||||
export function straightInsertSort(arr: number[], len: number) {
|
||||
// 重新确定数组长度
|
||||
len = arr.length === len ? len : arr.length
|
||||
if (arr.length !== len) {
|
||||
len = arr.length
|
||||
}
|
||||
|
||||
// 从第二个元素开始循环,共len-1次
|
||||
for (let i = 1; i < len; i++) {
|
||||
@@ -12,10 +14,12 @@ function straightInsertSort(arr, len) {
|
||||
let 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
|
||||
@@ -26,7 +30,7 @@ function straightInsertSort(arr, len) {
|
||||
}
|
||||
|
||||
const dealArr = [5, 2, 7, 3, 18, 8, 12, 1]
|
||||
|
||||
console.log('插入排序前:', dealArr)
|
||||
const sortResult = straightInsertSort(dealArr, 7)
|
||||
|
||||
console.log('插入排序后:', sortResult)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
version: '2'
|
||||
services:
|
||||
408CSFamily:
|
||||
image: registry.cn-hangzhou.aliyuncs.com/142vip/doc_book:408CSFamily-0.0.1
|
||||
image: registry.cn-hangzhou.aliyuncs.com/142vip/docs:408CSFamily-0.0.1-alpha.16
|
||||
container_name: 408CSFamily
|
||||
restart: on-failure
|
||||
hostname: 408CSFamily
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
"compileOnSave": true,
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"moduleDetection": "force",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"baseUrl": ".",
|
||||
|
||||
Reference in New Issue
Block a user