一 ,排序算法,
1.冒泡排序; 复杂度 O(n*n)
function bubbleSort(arr){
const length = arr.length;
let hasChange = true;
if(length<=1) return arr;
for(let i = 0 ; i < length-1 ;i++){
hasChange=false
for(let j = 0 ; j < length-i-1; j++){
if(arr[j]>arr[j+1] ){
[arr[j+1] ,arr[j]] =[arr[j],arr[j+1]];
hasChange=true;
}
}
if(!hasChange) break;
}
return arr ;
};
var arr1 = [1,2,7,8,9,0,4,6,7,8];
bubbleSort(arr1)
// [0, 1, 2, 4, 6, 7, 7, 8, 8, 9]
2. 快速排序 O(nlogn)
function QuickSort(arr){
if(arr.length<=1) return arr;
const index = Math.floor(arr.length/2);
const targetValue = arr.splice(index,1)[0];
const left = [];
const right =[];
arr.forEach(i=>{
if(i>targetValue){
right.push(i)
return
}
left.push(i)
})
return [...QuickSort(left),targetValue,...QuickSort(right)];
}
3.插入排序(n*n)
function insertionSort(arr){
const len = arr.length;
let cur;
for(let i=0;i<len;i++){
let preIndex = i-1;
cur = arr[i];
while(preIndex>=0 && arr[preIndex]>cur){
arr[preIndex+1]=arr[preIndex];
preIndex--
}
arr[preIndex+1] = cur;
}
return arr ;
}
var arr = [3,5,7,1,4,56,12,78,25,0,9,8,42,37];
insertionSort(arr);
// [0, 1, 3, 4, 5, 7, 8, 9, 12, 25, 37, 42, 56, 78]
4. 归并排序(nlogn)
so:1 Failed to load resource: net::ERR_TUNNEL_CONNECTION_FAILED
const mergeSort= arr =>{
const len =arr.length;
if(len<=1) return arr ;
const mid = Math.floor(len/2);
const left = arr.slice(0,mid);
const right = arr.slice(mid);
return merge(mergeSort(left),mergeSort(right));
}
function merge (left ,right){
const result = [] ;
while(left.length&&right.length){
if(left[0]<=right[0]){
result.push(left.shift())
} else{
result.push(right.shift())
}
}
while(left.length){
result.push(left.shift())
}
while(right.length){
result.push(right.shift())
}
return result ;
}
var arr = [3,5,7,1,4,56,12,78,25,0,9,8,42,37];
mergeSort(arr);
5. 深度优先遍历(DFS)
/*深度优先遍历三种方式*/
let deepTraversal1 = (node, nodeList = []) => {
if (node !== null) {
nodeList.push(node)
let children = node.children
for (let i = 0; i < children.length; i++) {
deepTraversal1(children[i], nodeList)
}
}
return nodeList
}
let deepTraversal2 = (node) => {
let nodes = []
if (node !== null) {
nodes.push(node)
let children = node.children
for (let i = 0; i < children.length; i++) {
nodes = nodes.concat(deepTraversal2(children[i]))
}
}
return nodes
}
// 非递归
let deepTraversal3 = (node) => {
let stack = []
let nodes = []
if (node) {
// 推入当前处理的node
stack.push(node)
while (stack.length) {
let item = stack.pop()
let children = item.children
nodes.push(item)
// node = [] stack = [parent]
// node = [parent] stack = [child3,child2,child1]
// node = [parent, child1] stack = [child3,child2,child1-2,child1-1]
// node = [parent, child1-1] stack = [child3,child2,child1-2]
for (let i = children.length - 1; i >= 0; i--) {
stack.push(children[i])
}
}
}
return nodes
}
6.广度优先遍历
let widthTraversal = (node) => {
let nodes = []
let stack = []
if (node) {
stack.push(node)
while (stack.length) {
let item = stack.shift()
let children = item.children
nodes.push(item)
// 队列,先进先出
// nodes = [] stack = [parent]
// nodes = [parent] stack = [child1,child2,child3]
// nodes = [parent, child1] stack = [child2,child3,child1-1,child1-2]
// nodes = [parent,child1,child2]
for (let i = 0; i < children.length; i++) {
stack.push(children[i])
}
}
}
return nodes
}