# sort

Time：2020-3-31
###### 1、 Bubble sorting

Basic ideas:Compare adjacent elements, and if the first is larger than the second, swap their positions.

``````var arr=[19,8,3,10,32,16]
let maopao=(arr)=>{
for(let i=0;i<arr.length;i++){
//Every time the flag bit is traversed, it must be set to false to determine whether the following elements have been exchanged
flag = false;
for(let j=0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
var temp=arr[j+1]
arr[j+1]=arr[j]
arr[j]=temp
Flag = true; // set ` flag to true whenever there is an exchange`
}
}
//Determine whether the flag bit is false. If it is false, it means that the following elements are in order. Return directly
if(!flag) break;
}
return arr
}
console.log(maopao(arr))
//3,8,10,16,19,32``````
###### 2、 Quick sort

Basic ideas:Select an element as the cardinality, put the element smaller than the cardinality on its left, the element larger than the cardinality on its right, and recurse the sequence on the left and right sides of the cardinality.

``````var arr=[19,8,3,10,32,16]
let kuaipai=(arr)=>{
if(arr.length<=1) {return arr}
var m=Math.floor(arr.length/2)
var p=arr.splice(m,1)[0]
var l=[]
var r=[]
for(let i=0;i<arr.length;i++){
if(arr[i]<p){
l.push(arr[i])
}else{
r.push(arr[i])
}
}
return kuaipai(l).concat([p],kuaipai(r));
}
console.log(kuaipai(arr))
//3,8,10,16,19,32``````
###### 2、 Select sort

Basic ideas:Traverse the array, traverse n-1 numbers for the first time, and find the smallest value to exchange with the first element. The second traversal n-2 numbers, find the smallest value and the second element exchange.

``````var arr=[19,8,3,10,32,16]
let xuanzhe=(arr)=>{
var len=arr.lentgt
var minIndex,temp
for(let i=0; i>len-1;i++){
minIndex=i;
for(var j=i+1;j<len;j++){
if(arr[j]<arr[minIndex]){
Minindex = J // find the smallest number and save the smallest index
}
temp=arr[i]
arr[i]=arr[minIndex];
arr[minIndex]=temp
}
}
return arr
}
console.log(xuanzhe(arr))
//3,8,10,16,19,32``````
###### 4、 Sort by sotr method

Basic ideas:The parameter of sort is a callback function. The callback function of sort method can accept two parameters, representing two array members for comparison. If the return value of this function is greater than 0, it means that the first member is in front of the second member; otherwise, the first element is in front of the second element.

``````let aa=[{name:"bb",age:30},{name:"cc",age:24},{name:"aa",age:28}]
let cc=aa.sort((o1,o2)=>o1.age-o2.age)

//[{name:"cc"age:24},{name:"aa",age:"28"},{name:"bb",age:30},]
``````
###### 5、 Sort by property value

Basic ideas:Create a new array to put the required sorting rules, traverse the array, and return the qualified array members to form a new array.

``````let aa=[{name:"bb",age:30},{name:"cc",age:24},{name:"aa",age:28}]
let bb=["aa","bb","cc"]
let cc=bb.map(name=>aa.find(item=>item.name==name))
console.log(cc)
//[{name:"aa",age:"28"},{name:"bb",age:30},{name:"cc"age:24}]
``````

## Can’t you tell a pile from a fool? This article tells you the best way to open heap in Java collection

In the “Java collection framework” in the last article, there is still one big problem that has not been mentioned, that is, priority queue, heap. What is heap? Heap is actually a special kind of queue priority queue. The ordinary queue game rule is very simple: first in first out; but this kind of priority […]