Front-end algorithm library collection

1 Preface 
front-end collection algorithm code library 

designed to help you raise the level of javascript coding, code specification, algorithmic problems facing the interviewer asked the most difficult can calmly 

This is a common algorithm interview questions js library collection includes testing, welcome star, if not in the library of algorithms, welcome to mention issue or PR, completion. 
Mentioned algorithm, here we must say that at the time complexity. 
Time Complexity: The time complexity of the algorithm is a function that describes the running time of the algorithm. The lower the time complexity, the higher the efficiency. 
2 With respect to code specifications 
As the saying goes, no rules no standards, so usually have to develop good coding habits 

Code Guide 
things (specification) required before the development js

 3 . About code tests 
of learning and continuous integration testing (Continuous Integration, referred to CI, meaning that, in a project, any person of any changes to the code base will trigger CI server automatically the project to build, automatically run the test, and even automatically deployed to the test environment. the advantage of this is that, at any time found the problem, ready to fix because the cost to fix the problem over time and growth, earlier found that the lower repair costs). 

javascript CI articles 
testing framework Mocha tutorial examples of 
past life karma of this life test framework

 4 . common algorithm
 4.1 binary search 
algorithm introduced 
a binary search, also known as binary search to find a specific element in an ordered array search algorithm. Find the process can be divided into the following steps: 
(1 ) First, start the search from the middle of the elements of an ordered array, if that element happens to be the target element (ie the element you want to find), the search process ends, otherwise the next step. 
( 2 half region) If the target element is greater or less than the intermediate element in the array is greater or less than the intermediate element lookup, then repeat step operation. 
( 3 ) If a step in the array is empty, then could not find the target element. 
Reference code: 
a non-recursive algorithm 
function binary_search (ARR, Key) {
   var Low = 0 , 
  High =-arr.length. 1 ;
   the while (Low <= High) {
      var MID = the parseInt ((High + Low) / 2);
      IF (Key == ARR [MID]) {
         return MID; 
     } the else  IF (Key> ARR [MID]) { 
        Low = MID +. 1 ; 
     }the else  IF (Key < ARR [MID]) { 
        High =-MID. 1 ; 
    } the else {
       return -1 ; 
    } 
  } 
}; 
var ARR = [1,2,3,4,5,6,7,8,9, 10,11,23,44,86 ];
 var Result = binary_search (ARR, 10 ); 
Alert (Result); // . 9 returns the index of the target element 
copy the code recursive algorithm
 function binary_search (ARR, Low, High, Key) {
   IF (Low> High) {
     return -1 ;    
  } 
  var MID = the parseInt ((High + Low) / 2);
   IF (ARR [MID] == Key) {
    return MID; 
  } the else  IF (ARR [MID]> Key) { 
    High = MID-. 1 ;
     return binary_search (ARR, Low, High, Key); 
  } the else  IF (ARR [MID] < Key) { 
    Low = MID +. 1 ;
     return binary_search (ARR, Low, High, Key); 
  } 
}; 
var ARR = [1,2,3,4,5,6,7,8,9,10,11,23,44,86 ];
 var binary_search = Result (ARR, 0,13,10 ); 
Alert (Result); // . 9 returns the index of the target element 
copy the code 4.2 sort
 4.2.1 bubble sort 
algorithm introduced 
Analysis:

Comparison of two adjacent elements, if one greater than the former, then the switch position. 
When the first round of the last element should be the biggest one. 
Compare two adjacent elements in accordance with Step one, this time because the last element is already the largest, so the last element without comparison. 

js code implementation 
function bubble_sort (ARR) {
   for ( var I = 0; I <-arr.length. 1; I ++ ) {
     for ( var J = 0; J <-I-arr.length. 1; J ++ ) {
       IF (ARR [J]> ARR [J +. 1 ]) {
         var the swap = ARR [J]; 
        ARR [J] = ARR [J +. 1 ]; 
        ARR [J + 1'd] = the swap; 
      } 
    } 
  } 
} 

var ARR = [. 3 , 1,5,7,2,4,9,6,10,8 ]; 
bubble_sort (ARR);
console.log (arr); 
duplicated code 4. 2 .2 Quicksort 
js code for 
parsing: Quicksort is an improvement of the bubble sort, the sort will trip the first data into two portions, all portions other than the data to be small. Then the recursive call, both sides have implemented quickly sorted. 
function quick_sort (ARR) {
   IF (arr.length <=. 1 ) {
     return ARR; 
  } 
  var pivotIndex = Math.floor (arr.length / 2);
   var Pivot = arr.splice (pivotIndex,. 1) [0 ]; 

  var = left [];
   var right = [];
   for ( var I = 0; I <arr.length; I ++ ) {
     IF (ARR [I] < Pivot) { 
      left.push (ARR [I]); 
    } the else {
      right.push (ARR [I]);
    } 
  } 

  Return quick_sort (left) .concat ([Pivot], quick_sort (right)); 
} 

var ARR = [5,6,2,1,3,8,7,1,2,3,4,7 ]; 
console.log (quick_sort (arr)); 
duplicated code 4. 2.3 insertion sort 
algorithm introduced 
Analysis: 

starting with the first element, which can be considered to have been sorted 
fetches the next element in the sequence of elements has been ordered from the rearward before scanning 
, if the element (sorted) is greater than the new element, the element to the next position 
repeat step 3 until you find the position of the sorted elements of the new elements less than or equal 
to the new element is inserted into the next position 
repeat step 2 

js code implementation 
function insert_sort (ARR) {
   var I =. 1 , 
  J, Key, len = arr.length;
   for (; I <len; I ++ ) {
     var J = varI;
     Key = ARR [J];
     the while (--j> -1 ) {
       IF (ARR [J]> Key) { 
        ARR [J + 1'd] = ARR [J]; 
      } the else {
         BREAK ; 
      } 
    } 

    ARR [ J +1] = Key; 
  } 

  return arr; 
} 

insert_sort ([ 2,34,54,2,5,1,7 ]); 

author: geeks tutorial 
link: HTTPS: // juejin.im/post/5aa4cd1051882577b45ea2c3 
sources : Nuggets 
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

 

Guess you like

Origin www.cnblogs.com/hjptopshow/p/11428304.html