1. Time complexity is the number of while, and two points to find O (h) =o (log2n) 2. The breadth-first traversal of a node
function Traverse (Root) { = [root]; while (queue.length) { = queue.shift (); Printinfo (node); if (! node.children.length) { continue; } Array.from (Node.children). ForEach (x=queue.push (x));} } function printinfo (node) { Console.log (node.nodename, Node.classname)}traverse (root)
3. Depth-first traversal of the DOM tree
function printinfo (node, layer) { Span style= "COLOR: #0000ff" >var str = "for (Let i = 1; i < layer; I++
+ = '
} console.log (' ${layer}:${str}${node.tagna Me}.${node.classname} ');}
function dfs = (rootnodes, rootlayer) =>
{const Roots =
Array.from (rootnodes)
while
(roots.length) {const root =
Roots.shift () ; Printinfo (Root, Rootlayer);
if
(root.children.length) {DFS ( Root.children, Rootlayer + 1
4. Bubble sort (O (n^2))
It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.
function Bubblesort (arr) { = arr.length; for (Let i = 0; i < len; i++.) {for (let j = 0; J < len-1-I; k + +) { if(a RR[J] > arr[j + 1]) { [arr[j], Arr[j+1]] = [arr[j+1], arr[j]];}} } return arr;}
5. Quick-Line (O (NLOGN))
Divide the sorted data into separate parts by a single trip, where all of the data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, and the whole sort process can be recursive, To achieve the entire data into an ordered sequence.
var quickSort = function if (arr.length <= 1) { return arr; Const Midindex = Math.floor (Arr.length/2 = Arr.splice (Midindex, 1) [0 = [], right = []; Arr.foreach ( function (item) { Span style= "COLOR: #0000ff" >if (item < mid) {Left.push (item); else {right.push (item); }}) return QuickSort (left). Concat ([mid], QuickSort (right));}
A review of the JS implementation of sorting algorithm