Advertisement
Sitisom

js algorithms

May 29th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function BinarySearch(t,A){                              
  2.     var i = 0, j = A.length, k;
  3.     while (i < j){  
  4.         k = Math.floor((i+j)/2);
  5.         if (t <= A[k]) j = k;
  6.         else i = k+1;
  7.     }
  8.    
  9.     if (A[ i ] === t) return i;    
  10.     else return -1;                
  11. }
  12. function quickSort(arr[], left, right) {
  13.     let i = left, j = right;
  14.     const pivot = arr[(left + right) / 2];
  15.  
  16.     while (i <= j) {
  17.         while (arr[i] < pivot) i++;
  18.         while (arr[j] > pivot) j--;
  19.         if (i <= j) {
  20.             swap(arr[i], arr[j]);
  21.             i++;
  22.             j--;
  23.         }
  24.     }
  25.     if (left < j)
  26.         quickSort(arr, left, j);
  27.     if (i < right)
  28.         quickSort(arr, i, right);
  29. }
  30.  
  31. function bubbleSort(arr[], l) {
  32.     for (let i = 0; i < l; i++)
  33.         for (let j = (l - 1); j >= (i + 1); j--) {
  34.             if (arr[j] < arr[j - 1])
  35.                 swap(arr[j], arr[j - 1]);
  36.         }
  37. }
  38.  
  39. function selectionSort(arr[], l) {
  40.     let min;
  41.     for (let i = 0; i < l-1; i++) {
  42.         min = i;
  43.         for (let j = i+1; j < l; k++)
  44.             if (arr[min] > arr[j])
  45.                 min = j;
  46.         swap(arr[i], arr[min]);
  47.     }
  48. }
  49.  
  50. function insertionSort(arr[], l) {
  51.     let k = 0, i = 0;
  52.     for (let j = 1; j < l; j++) {
  53.         k = arr[j];
  54.         i = j - 1;
  55.         while (i >= 0 && arr[i] > k) {
  56.             arr[i + 1] = arr[i];
  57.             i -= 1;
  58.             arr[i + 1] = k;
  59.         }
  60.     }
  61. }
  62. const merge = (arrFirst, arrSecond) => {
  63.     const arrSort = [];
  64.     let i = j = 0;
  65.  
  66.     while (i < arrFirst.length && j < arrSecond.length) {
  67.         arrSort.push(
  68.             (arrFirst[i] < arrSecond[j]) ?
  69.                 arrFirst[i++] : arrSecond[j++]
  70.         );
  71.     }
  72.     return [
  73.         ...arrSort,
  74.         ...arrFirst.slice(i),
  75.         ...arrSecond.slice(j)
  76.     ];
  77. };
  78. const mergeSort = arr => {
  79.     if (!arr || !arr.length) {
  80.         return null;
  81.     }
  82.     if (arr.length <= 1) {
  83.         return arr;
  84.     }
  85.     const middle = Math.floor(arr.length / 2);
  86.     const arrLeft = arr.slice(0, middle);
  87.     const arrRight = arr.slice(middle);
  88.  
  89.     return merge(mergeSort(arrLeft), mergeSort(arrRight));;
  90. };
  91.  
  92. class BinarySearchTree {
  93.   constructor(){
  94.     this.root = null;
  95.   }
  96.  
  97.   add(value){
  98.     let newNode = { value, left: null, right: null};
  99.    
  100.     // set the root if we don't have one
  101.     if(this.root == null){
  102.       this.root = newNode;
  103.       return;
  104.     }
  105.    
  106.     let current = this.root;
  107.    
  108.     while(true){
  109.       if(value > current.value){
  110.         if(!current.right){ current.right = newNode; break; }
  111.         current = current.right;
  112.       } else if(value < current.value){
  113.         if(!current.left){ current.left = newNode; break; }
  114.         current = current.left;
  115.       } else {
  116.         break;
  117.       }
  118.     }
  119.   }
  120.   contains(value){
  121.     let current = this.root;
  122.     while(current){
  123.       if(current.value == value){
  124.         return true;
  125.       } else if (current.value > value){
  126.         current = current.left;
  127.       } else {
  128.         current = current.right;
  129.       }
  130.     }
  131.     return false;
  132.   }
  133. }
  134. class DepthFirstTraverser extends BinarySearchTree {
  135.   consstructor(){
  136.     this.traverseMethod = 'pre-order';
  137.   }
  138.  
  139.   setTraverseMethod(traverseMethod){
  140.     if(traverseMethod == 'pre-order' || traverseMethod == 'in-order' || traverseMethod == 'post-order'){
  141.       this.traverseMethod = traverseMethod;
  142.     } else {
  143.       console.error('Not a valid search method, must be "pre-order", "in-order" or "post-order"');
  144.     }
  145.   }
  146.  
  147.   getTraverseMethod(){
  148.     return this.traverseMethod;
  149.   }
  150.  
  151.   traverse(){
  152.     switch(this.traverseMethod){
  153.       case 'pre-order':
  154.         this.preOrderTraverse(value);
  155.         break;
  156.       case 'in-order':
  157.         this.inOrderTraverse(value);
  158.         break;
  159.       case 'post-order':
  160.         this.postOrderTraverse(value);
  161.         break;
  162.       default:
  163.         console.error('invalid traverse method');
  164.     }
  165.   }
  166.  
  167.   preOrderTraverse(value){
  168.     if(value){
  169.         console.log(value.value);
  170.         preOrderTraverse(value.left);
  171.         preOrderTraverse(value.right);
  172.   }
  173.  
  174.   inOrderTraverse(value){
  175.  
  176.   }
  177.  
  178.   postOrderTraverse(value){
  179.  
  180.   }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement