Skip to content

Commit

Permalink
feat: 优化ts版本算法代码,删除冗余算法代码,基于Eslint格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Nov 1, 2024
1 parent f1574b8 commit dcec9ad
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 270 deletions.
43 changes: 0 additions & 43 deletions code/ds/BinaryInsertSort.js

This file was deleted.

37 changes: 0 additions & 37 deletions code/ds/BubbleSort.js

This file was deleted.

52 changes: 0 additions & 52 deletions code/ds/QuickSort.js

This file was deleted.

79 changes: 0 additions & 79 deletions code/ds/ShellSort.js

This file was deleted.

32 changes: 0 additions & 32 deletions code/ds/StraightInsertSort.js

This file was deleted.

13 changes: 8 additions & 5 deletions code/ds/ts版本/BinaryInsertSort.ts
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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)
17 changes: 9 additions & 8 deletions code/ds/ts版本/BubbleSort.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -25,26 +29,23 @@ function BubbleSort(arr: Array<number>): number[] {
}
}
// 第一趟比较后,如果本身序列是有序的,就直接跳出循环
if (isSorted === false) {
if (!isSorted) {
break
}
}

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)
Expand Down
4 changes: 2 additions & 2 deletions code/ds/ts版本/QuickSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit dcec9ad

Please sign in to comment.