@ryanuo/utils
Version:
提供多种实用工具函数,涵盖算法、浏览器操作、网络请求等多个领域
82 lines (81 loc) • 2.29 kB
TypeScript
/**
* 封装通用或特定领域算法,解决计算、排序、加密等逻辑问题。
* @module algorithm
*/
/**
* 冒泡排序(Bubble Sort)
* @category Algorithm
* @example
* ```ts twoslash
* import { bubbleSort } from '@ryanuo/utils'
* const sorted = bubbleSort([5, 3, 8, 4, 2])
* console.log(sorted) // [2, 3, 4, 5, 8]
* ```
* @param arr 数组
* @returns 排序后的数组
*/
export declare function bubbleSort(arr: number[]): number[];
/**
* 快速排序(Quick Sort)
* @category Algorithm
* @example
* ```ts twoslash
* import { quickSort } from '@ryanuo/utils'
* const sorted = quickSort([5, 3, 8, 4, 2])
* console.log(sorted) // [2, 3, 4, 5, 8]
* ```
* @param arr 数组
* @returns 排序后的数组
*/
export declare function quickSort(arr: number[]): number[];
/**
* 二分查找(Binary Search)
* @category Algorithm
* @example
* ```ts twoslash
* import { binarySearch } from '@ryanuo/utils'
* const index = binarySearch([1, 2, 3, 4, 5], 3)
* console.log(index) // 2
* ```
* @param arr 已排序的数组
* @param target 查找目标
* @returns 如果找到返回索引,否则返回 -1
*/
export declare function binarySearch(arr: number[], target: number): number;
/**
* 判断一个数是否为质数(Prime Number Check)
* @category Algorithm
* @example
* ```ts twoslash
* import { isPrime } from '@ryanuo/utils'
* console.log(isPrime(11)) // true
* console.log(isPrime(15)) // false
* ```
* @param num 数字
* @returns 是否为质数
*/
export declare function isPrime(num: number): boolean;
/**
* 斐波那契数列(Fibonacci Sequence) 动态规划
* @category Algorithm
* @example
* ```ts twoslash
* import { fibonacciDP } from '@ryanuo/utils'
* console.log(fibonacciDP(10)) // 55
* ```
* @param n 数列的索引
* @returns 斐波那契数列的第 n 项
*/
export declare function fibonacciDP(n: number): number;
/**
* 斐波那契数列(递归版)
* @category Algorithm
* @example
* ```ts twoslash
* import { fibonacciRecursive } from '@ryanuo/utils'
* console.log(fibonacciRecursive(10)) // 55
* ```
* @param n 数列的索引
* @returns 斐波那契数列的第 n 项,若 n 为负数则返回 0
*/
export declare function fibonacciRecursive(n: number): number;