A review of the JS implementation of sorting algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.