当前位置:网站首页>Ten sorting details
Ten sorting details
2022-07-26 19:50:00 【Jiangjiangchun】

Bubble sort (n2)
The first layer circulates through the outer layer length -1 The item , To the front length-1 Number sorting
The inner loop traverses to length - i -1, To the front length - i -1 Number bubbling
function bubbleSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
const temp = arr[j + 1]
arr[j + 1] = arr[j]
arr[j] = temp
}
}
}
Selection sort (n2)
Select the minimum joining sequence area from the disordered sequence area
The outer loop : Expand the disordered area to length-1
Inner circulation : Select the minimum subscript in the disordered area
function selectSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
let minIndex = i
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j
}
}
const tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
}Insertion sort (n2)
Choose any one from the disordered areas , Find its position in the disordered area
The outer loop : Expand ordered areas , To length-1
Inner circulation : Find the insertion position in the ordered area
use pre and current Assist in finding location
function insertionSort(arr) {
for (let i = i; i < arr.length - 1; i++) {
let preIndex = i - 1
let current = arr[i]
while (preIndex > 0 && arr[preIndex] > current) {
arr[preIndex + 1] = arr[preIndex]
preIndex--
}
}
arr[preIndex + 1] = current
}Shell Sort (nlogn~nlog2n)
use gap Split the array , Insert and sort each small array
Outer circulation :gap Half off each time , until gap by 0
function shellSort(arr) {
for (let gap = Math.floor(arr.length); gap > 0; gap = Math.floor(arr / 2)) {
for (let i = gap; gap < arr.length; i++) {
const curr = arr[i]
let j = i
while (j - gap > 0 && arr[j-gap] >curr) {
arr[j]=arr[j-gap]
j=j-gap
}
arr[j]=curr
}
}
}Merge sort (nlogn)
Standard divide and conquer , Divided into two arrays , Recursive solution .
function mergeSort(arr) {
function sort(arr, left, right) {
if (left < right) {
const mid = (left + right) / 2
leftArr = sort(arr, left, mid)
rightArr = sort(arr, mid + 1, right)
return merge(leftArr, rightArr)
}
return left > 0 ? [arr[left]] : []
}
function merge(leftArr, leftArr) {
const res = []
const leftPtr = 0
const rightPtr = 0
while (leftPtr < leftArr.length && rightPtr < rightArr.length) {
if (leftArr[leftPtr] < leftArr[rightPtr]) {
res.push(leftArr[leftPtr++])
} else {
res.push(rightArr[rightPtr++])
}
}
while (leftPtr < leftArr.length) {
res.push(leftArr[leftPtr++])
}
while (rightPtr < rightArr.length) {
res.push(rightArr[rightPtr++])
}
return res
}
}Quick sort (nlogn-n2)
Divide and conquer method , Select the first element , Divide the array into a group larger than the element and a group smaller than the element . Recursive solution
It mainly includes four steps
Back and forth , Find a comparison x Small
In exchange for
After going , Find a comparison x Big
In exchange for
function quickSort(arr) {
function sort(arr, low, high) {
let i = low
let j = high
const x = arr[low]
while (i < j) {
while (i < j && arr[j] > x) {
j--
}
if (i < j) {
arr[i] = arr[j]
i++
}
while (i < j && arr[i] < x) {
i++
}
if (i < j) {
arr[j] = arr[i]
j--
}
}
arr[i] = x
sort(arr, low, i - 1)
sort(arr, i + 1, high)
}
}Count sorting (n+k)
Determine the array range , Open up a continuous space , One time storage , Read in spatial order
It's like a hash table , Store the subscript as a value
// Count sorting
function countingSort(arr) {
let maxValue = Number.MIN_VALUE
let minValue = Number.MAX_VALUE
const res = []
// Determine the space
arr.forEach(num => {
maxValue = num > maxValue ? num : maxValue
minValue = num > minValue ? minValue : num
})
if (minValue < 0) {
offset = -minValue
}
const bucket=new Array(maxValue+offset+1).fill(0)
// Deposit the corresponding subscript
arr.forEach(num=>{
bucket[num+offset]++
})
bucket.forEach((store,index)=>{
while(store--){
res.push(index-offset)
}
})
return result
}Radix sorting
Put each one in a different bucket 
Heap sort 
Big pile top : The root node is larger than children

1、 Pre function , Maintain the nature of the big top pile
Swap the root node with the maximum
Recursively maintain the properties of the exchanged node

2、 Build the big top heap
Start with the parent of the first leaf node :n - 2 / 2 = n / 2 - 1
Traverse every node , Conduct property maintenance
3、 Sort
Swap the head and tail
Then disconnect the tail connection
Maintain the large top pile
At this time, the tail is the largest

边栏推荐
- Adjust the array order so that odd numbers precede even numbers and their relative positions remain the same
- 客户案例|生学教育依托观测云打造可观测智慧教育新生态
- EN 1504-7 products for protection and repair of concrete structures corrosion prevention of reinforcement - CE certification
- [PHP] MySQL native PHP operation - Tianlong eight steps
- 通过源码深度分析线程池中Worker线程的执行流程
- Pyqt5 rapid development and practice 3.6 packaging resource files
- 工作13年后,个人的一点软件测试经历及感想……
- C# .net 时间戳和时间转换 支持时区
- openstack 虚拟机网卡被重名为cirename0
- 【PHP】常用的header头部定义
猜你喜欢

中天钢铁在 GPS、 AIS 调度中使用 TDengine

Detailed explanation of Yolo V2

线性代数第3章向量

J1: why is redis so fast + basic structure

eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c

ShardingSphere-JDBC 关键字问题

服务器内存故障预测居然可以这样做

线性代数第4章线性方程组

基于华为云 IOT 设计智能称重系统 (STM32)【二】结尾有资料

Ijcai2022 meeting! Brescia et al. Tutorial of evidential reasoning and learning, describing its latest progress, with 96 slides attached
随机推荐
Detailed explanation of Yolo v1
【OBS】Dropped Frames And General Connection Issues
win11 edge怎么卸载?win11 edge浏览器彻底卸载的方法教程
NLP learning path
客户案例|生学教育依托观测云打造可观测智慧教育新生态
拿铁DHT-PHEV产品响当当,销量会不会让李瑞峰想通了呢?
Design of intelligent weighing system based on Huawei cloud IOT (STM32) [II] there is information at the end
中天钢铁在 GPS、 AIS 调度中使用 TDengine
How to compress the traffic consumption of APP under mobile network in IM development
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
CIO guide to business change
原 iOS面试题收集
Latte dht-phev products are very popular. Will the sales volume make Li Ruifeng figure it out?
Intensive reading of the paper: yolov2 - yolo9000: better, faster, stronger
亲力亲为的思考
基于ABP实现DDD--领域逻辑和应用逻辑
TB 117-2013 US Federal mandatory regulations
MySQL 子查询使用方式
Fair lock process of reentrantlock learning
MySQL tutorial: MySQL database learning classic (from getting started to mastering)