当前位置:网站首页>用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;
}
边栏推荐
- How to make the minimum API bind the array in the query string
- Mysql database
- The issuing process of individual developers applying for code signing certificates
- Reflection on pytorch back propagation
- DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒
- 【11】 Binary code: "holding two roller handcuffs, crying out for hot hot hot"?
- Interpretation of deepsort source code (II)
- Fix the problem that the paging data is not displayed when searching the easycvr device management list page
- How can chrome quickly transfer a group of web pages (tabs) to another device (computer)
- 脱氧核糖核酸DNA改性近红外二区砷化镓GaAs量子点|GaAs-DNA QDs|DNA修饰GaAs量子点
猜你喜欢

Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene

Hospital reservation management system based on SSM

Student status management system based on SSM

newest! SASAC releases new measures for digital transformation of state-owned enterprises

PNA polypeptide PNA TPP | GLT ala ala Pro Leu PNA | suc ala Pro PNA | suc AAPL PNA | suc AAPM PNA

Campus news release management system based on SSM

Bert and RESNET can also be trained on mobile phones?!

nvidia-smi 各参数意义

Gbase 8C product introduction

Fix the problem that the paging data is not displayed when searching the easycvr device management list page
随机推荐
齐岳:巯基修饰寡聚DNA|DNA修饰CdTe/CdS核壳量子点|DNA偶联砷化铟InAs量子点InAs-DNA QDs
Book borrowing management system based on SSM
O2O电商线上线下一体化模式分析
Introduction to the official functions of easyrecovery14 data recovery software
Interpretation of deepsort source code (III)
How to make the minimum API bind the array in the query string
Gbase 8C - SQL reference 6 SQL syntax (13)
newest! SASAC releases new measures for digital transformation of state-owned enterprises
Interpretation of deepsort source code (VII)
Talk about multimodality of fire
2021 interview questions for php+go of Zhongda factory (2)
CdS quantum dots modified DNA | CDs DNA QDs | near infrared CdS quantum dots coupled DNA specification information
(转帖)eureka、consul、nacos的对比2
Mysql database
Pytorch model
deepsort源码解读(六)
运行代码报错: libboost_filesystem.so.1.58.0: cannot open shared object file: No such file or directory
脱氧核糖核酸DNA改性近红外二区砷化镓GaAs量子点|GaAs-DNA QDs|DNA修饰GaAs量子点
TS learning (VIII): classes in TS
Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene