当前位置:网站首页>用typescript实现排序-递增
用typescript实现排序-递增
2022-07-27 05:45:00 【翊飞】
var arr= [1,3,5,9,2,7,4,6,8]
arr.sort() --默认是从小到大排序
// 冒泡排序 bubbleSort
function sortFun(arr:Array<number>):Array<number>{
for(let i=0;i<arr.length;i++){
for(let j=0;j<arr.length - i;j++){
let temp
if (arr[j] < arr[j+1]){
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
return arr
}
// 快速排序,quickSort
function quickSort(arr: Array<number>): Array<number> {
let final: Array<number> = new Array(...arr);
function tureQuickSort(arr: Array<number>, left: number, right: number): Array<number> {
if (left >= right) {
return;
}
let i: number = left, j: number = right;
let mark: number = arr[i];
// 用mark作为中间值,将数组左右对半分
while (i < j) {
// 寻找右边比mark更小的
while (i < j && arr[j] >= mark) {
j--;
}
arr[i] = arr[j];
// 寻找左边比mark更大的
while (i < j && arr[i] <= mark) {
i++;
}
arr[j] = arr[i];
}
arr[i] = mark;
// 递归分左右两个子数组
tureQuickSort(arr, left, i - 1);
tureQuickSort(arr, i + 1, right);
}
tureQuickSort(final, 0, final.length - 1);
return final;
}
插入排序是维护一个长度为i的有序序列,每次i自增1,为下标为i的数向前找插入位置。
// 插入排序 insertSort
function insertSort(arr: Array<number>): Array<number> {
let final: Array<number> = new Array(...arr);
for (let i = 1; i < final.length; i++) {
let mark: number = final[i];
let j: number;
// 寻找插入位置
for (j = i - 1; j >= 0; j--) {
if (final[j] < mark) {
// 插入位置找到
break;
} else {
final[j + 1] = final[j];
}
}
// 进行插入操作
final[j + 1] = mark;
}
return final;
}
边栏推荐
- 基于SSM实现的校园新闻发布管理系统
- About the new features of ES6
- Interpretation of deepsort source code (I)
- Reasoning speed of model
- Analysis on the current situation and optimization strategy of customer experience management in banking industry
- 基于SSM学生成绩管理系统
- Pytorch uses data_ Prefetcher improves data reading speed
- regular expression
- Customer cases | focus on process experience to help bank enterprise app iteration
- Student achievement management system based on SSM
猜你喜欢

网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...

PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA

基于SSM医院预约管理系统

Express framework

Event capture and bubbling - what is the difference between them?

(转帖)eureka、consul、nacos的对比1

How can chrome quickly transfer a group of web pages (tabs) to another device (computer)

Mysql database

Derivative, partial derivative and gradient

基于SSM图书借阅管理系统
随机推荐
二叉树--天然的查找语义(1)基础篇
Pytorch model
Introduction to the official functions of easyrecovery14 data recovery software
How to delete or replace the loading style of easyplayer streaming media player?
CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法
Student achievement management system based on SSM
deepsort源码解读(五)
Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
Dimension problems and contour lines
强网杯2021 pwn 赛题解析——babypwn
Book borrowing management system based on SSM
李沐动手学深度学习V2-transformer和代码实现
deepsort源码解读(七)
DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒
DNA research experiment application | cyclodextrin modified nucleic acid cd-rna/dna | cyclodextrin nucleic acid probe / quantum dot nucleic acid probe
regular expression
Matlab drawing (ultra detailed)
Why can cross entropy loss be used to characterize loss
PNA peptide nucleic acid modified peptide suc Tyr Leu Val PNA | suc ala Pro Phe PNA 11
DNA modified noble metal nanoparticles | DNA modified gold nanoparticles (scientific research level)