Guest User

Untitled

a guest
Oct 24th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. const mergeSort = (arr) =>{
  2. if(arr.length < 2 ) {
  3. return arr
  4. }
  5. const middle = Math.floor(arr.length/2);
  6.  
  7. const right = arr.slice(0, middle);
  8. const left = arr.slice(middle, arr.length)
  9. return merge(mergeSort(left),mergeSort(right))
  10. }
  11.  
  12. const merge = (left, right)=>{
  13. const result = [];
  14.  
  15. while(left.length && right.length){
  16.  
  17. if(left[0] <= right[0]){
  18. result.push(left.shift());
  19. } else{
  20. result.push(right.shift());
  21. }
  22. }
  23.  
  24. return result.concat(left,right);
  25. }
  26. mergeSort([1,2,3,9,3,2,9,4,8,5])
  27.  
  28.  
  29. ////////////////////////////////// quicksort
  30.  
  31. const quicksort = (arr)=>{
  32. if ( arr.length <=1 ){
  33. return arr
  34. }
  35. const pivote = arr[arr.length -1]
  36. const right = []
  37. const left = []
  38.  
  39. for ( let i = 0; i < arr.length -1 ; i ++){
  40. if (arr[i] < pivote){
  41. left.push(arr[i])
  42. }
  43. else{
  44. right.push(arr[i])
  45.  
  46. }
  47.  
  48. }
  49. return [...quicksort(left),pivote,...quicksort(right)]
  50.  
  51.  
  52. }
  53. quicksort([1,2,4,3,2,21,1,1,15,2,3,4])
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. //////////// . binarySearch
  63. let a = [1,8,4,5,9]
  64.  
  65. //////////////////////////// . sorting arr
  66.  
  67. function bs(l,h,arr,key){
  68. let sArr = arr.sort((a,b)=>{
  69. return a > b
  70. })
  71.  
  72.  
  73. let middle = Math.floor((l+h+1/2))
  74.  
  75. ///////////////////////////// only 1 item in arr
  76. if( l === h ){
  77. if (sArr[l] === key){
  78. return true
  79. }
  80.  
  81. }
  82. else {
  83.  
  84. if (sArr[middle]=== key){
  85. return true
  86. }
  87. else if (key < sArr[middle]){
  88. return bs(l, middle-1,sArr,key)
  89. }
  90. else if (key > sArr[middle]){
  91. return bs(middle + 1, h ,sArr,key)
  92. }
  93. return false
  94. }
  95.  
  96.  
  97. }
  98. console.log(bs(0,a.length-1,a,8))
  99.  
  100. //////////////////////////////// . queue
  101.  
  102.  
  103. class Queue{
  104. constructor(){
  105. this.storage = {};
  106. this.size = 0
  107.  
  108. }
  109.  
  110. enqueue(value ){
  111.  
  112. this.storage[this.size ++] = value;
  113.  
  114. }
  115.  
  116. dequeue(){
  117. this.size--;
  118.  
  119. let deleted = delete this.storage[0];
  120. for( let key in this.storage){
  121.  
  122. this.storage[key -1] = this.storage[key]
  123. delete this.storage[key]
  124. }
  125. return deleted
  126.  
  127. }
  128.  
  129. getSize(){
  130. return this.size }
  131. }
  132.  
  133. const copy = new Queue()
  134. copy.enqueue('hilal');
  135. copy.enqueue('filal');
  136. copy.enqueue('bilal');
  137. copy.dequeue()
  138.  
  139.  
  140. console.log(copy.getSize())
  141.  
  142. ///////////////////////////////// stack
  143.  
  144.  
  145. class Stack{
  146. constructor(){
  147. this.storage = {};
  148. this.size = 0
  149.  
  150. }
  151.  
  152. add(value ){
  153.  
  154. this.storage[this.size ++] = value;
  155.  
  156. }
  157.  
  158. remove(){
  159. this.size && this.size--;
  160.  
  161. let deleted = delete this.storage[this.size];
  162.  
  163.  
  164. return deleted
  165.  
  166. }
  167.  
  168. getSize(){
  169. return this.size }
  170. }
  171.  
  172. const copy = new Stack()
  173. copy.add('hilal');
  174. copy.add('filal');
  175. copy.add('bilal');
  176. copy.remove()
  177. copy.add('kilal');
  178.  
  179.  
  180. console.log(copy)
  181.  
  182. ////////////////////////////// binary tree
  183.  
  184.  
  185. class BT{
  186.  
  187. constructor(value){
  188. this.value = value;
  189. this.left = null;
  190. this.right = null
  191.  
  192. }
  193.  
  194.  
  195. insert(x){
  196.  
  197. if( x < this.value && !this.left){
  198. this.left = new BT(x)
  199. }
  200.  
  201. if(x < this.value && this.left){
  202. this.left.insert(x);
  203. }
  204.  
  205. if(x > this.value && !this.right){
  206.  
  207. this.right = new BT(x)
  208.  
  209. }
  210.  
  211. if(x > this.value && this.right){
  212.  
  213. this.right.insert(x)
  214.  
  215. }
  216.  
  217. }
  218.  
  219. contains(x){
  220.  
  221. if (this.value === x){
  222. return true ;
  223. }
  224. return !! (this.left && this.left.contains(x))
  225. || !!(this.right && this.right.contains(x))
  226.  
  227. }
  228.  
  229. }
  230.  
  231.  
  232. ///////////////////////
  233.  
  234. class Node{
  235. constructor(value){
  236. this.value = value;
  237. this.next = null;
  238. }
  239. }
  240.  
  241. class LinkedList{
  242. constructor(){
  243. this.head = null;
  244. this.tail = null;
  245. this.size = 0;
  246. }
  247.  
  248.  
  249. /////////////////
  250.  
  251. add(value){
  252. const newNode = new Node(value)
  253.  
  254. if(!this.head){
  255. this.head = newNode;
  256. this.tail = newNode;
  257. this.size ++;
  258. return;
  259. }
  260. this.tail.next = newNode;
  261. this.tail = newNode;
  262. this.size ++;
  263. return;
  264. }
  265.  
  266. //////////////////
  267.  
  268. returnHead(){
  269. if(! this.head){
  270.  
  271. return null;
  272.  
  273. }
  274.  
  275. const head = this.head.value
  276. if(this.size === 1){
  277. this.head = null;
  278. this.tail = null;
  279. this.size --;
  280. return head;
  281. }
  282.  
  283. this.head = this.head.next
  284. this.size --;
  285. return head;
  286.  
  287.  
  288. }
  289. }
  290.  
  291. const copy = new LinkedList()
  292.  
  293. copy.add(6)
  294. console.log(copy.returnHead())
  295.  
  296. console.log(copy);
  297.  
  298.  
  299. /////////////////////////// possibilities
  300.  
  301. const pro = (rounds)=>{
  302.  
  303. const result = [];
  304. let combination;
  305. let possibilities = ['r', 's','z']
  306.  
  307. const helper = (str,rounds)=>{
  308. if(rounds === 0){
  309. result.push(str);
  310. return;
  311. }
  312.  
  313. for(let i = 0 ; i < possibilities.length ; i++){
  314. combination = str + possibilities[i]
  315. helper(combination, rounds - 1 );
  316. }
  317.  
  318. }
  319.  
  320. helper('',rounds);
  321. return result;
  322.  
  323. }
  324.  
  325. console.log(pro(3));
  326.  
  327. //////////////////////// combinations
  328.  
  329.  
  330. let string = "alfo"
  331.  
  332. let arr = string.split('');
  333. console.log(arr);
  334.  
  335.  
  336.  
  337. const pro = (arr)=>{
  338. const result = [];
  339. let combination;
  340.  
  341. const helper = (str,newArr)=>{
  342.  
  343. for(let i = 0 ; i < newArr.length; i++){
  344. combination = str + newArr[i]
  345. result.push(combination)
  346. helper(combination, newArr.slice(i+1));
  347. }
  348.  
  349. }
  350.  
  351. helper('',arr);
  352. return result;
  353.  
  354. }
  355.  
  356. console.log(pro(arr));
  357.  
  358. ////////// fibonacci
  359.  
  360.  
  361.  
  362.  
  363. let result;
  364. function naivefib(n){
  365. if ( n < 3 ){
  366. return 1
  367. }
  368. result = naivefib(n-1) + naivefib(n-2)
  369. return result;
  370. }
  371. console.log(naivefib(20))
  372.  
  373.  
  374. / memorized solution
  375.  
  376. let result
  377. let arr = [];
  378.  
  379. function fib(n){
  380. if (arr[n] !== null && arr[n]!== undefined){
  381. return arr[n]
  382. }
  383. if ( n < 3 ){
  384. return 1
  385. }
  386.  
  387. result = fib(n-1) + fib(n-2)
  388. arr[n]= result
  389. return result
  390.  
  391. }
  392. console.log(fib(1000))
  393.  
  394. //////// buttom up
  395.  
  396. let newarr = []
  397. function newfib(n){
  398.  
  399. newarr[1] = 1;
  400. newarr[2] = 1;
  401. for(let i = 3; i <= n ; i ++ ){
  402. newarr[i] = newarr[i-1] + newarr[i-2]
  403. }
  404.  
  405. return newarr[n]
  406. }
  407. console.log(newfib(1000))
  408.  
  409. ///////////////////////////////////
  410.  
  411. //////////////// .
  412.  
  413. class Heap{
  414. constructor(){
  415.  
  416. this.storage = [];
  417.  
  418.  
  419. }
  420.  
  421.  
  422. insert(x){
  423.  
  424.  
  425. if(this.storage.length > 0){
  426.  
  427. if( x > this.storage[0]){
  428. this.storage.unshift(x)
  429. }
  430. else {
  431. this.storage.push(x)
  432. this.bubleup(this.storage.length -1)
  433. }
  434.  
  435. }
  436.  
  437.  
  438.  
  439. else{
  440. this.storage.push(x)
  441.  
  442. }
  443.  
  444.  
  445. }
  446.  
  447.  
  448. delete(){
  449. if(this.storage.length === 0){
  450. return null;
  451. }
  452.  
  453. const max = this.storage.shift()
  454. this.siftdown(0)
  455. return max;
  456.  
  457.  
  458. }
  459.  
  460. bubleup(childIndex){
  461.  
  462. const parentIndex = Math.floor((childIndex -1)/2 )
  463. if( this.storage[parentIndex] < this.storage[childIndex]){
  464. [this.storage[parentIndex],this.storage[childIndex]] = [ this.storage[childIndex], this.storage[parentIndex]]
  465. this.bubleup(parentIndex);
  466. }
  467.  
  468. }
  469.  
  470.  
  471. siftdown(parentIndex){
  472.  
  473.  
  474. const rightChildIndex = parentIndex*2 + 1;
  475. const leftChildIndex = parentIndex*2 + 2;
  476. let maxIndex;
  477.  
  478.  
  479. if(this.storage[rightChildIndex] && this.storage[leftChildIndex]){
  480. maxIndex = this.storage[rightChildIndex] > this.storage[leftChildIndex] ? rightChildIndex : leftChildIndex;
  481.  
  482. if(this.storage[maxIndex] > this.storage[parentIndex]){
  483. [this.storage[maxIndex],storage[parentIndex]] = [this.storage[parentIndex],this.storage[maxIndex]]
  484. this.siftdown(maxIndex)
  485. }
  486. }
  487.  
  488. if(this.storage[rightChildIndex]){
  489. maxIndex = rightChildIndex;
  490. if(this.storage[maxIndex] > this.storage[parentIndex]){
  491. [this.storage[maxIndex],storage[parentIndex]] = [this.storage[parentIndex],this.storage[maxIndex]]
  492. this.siftdown(maxIndex)
  493. }
  494. }
  495. if(this.storage[leftChildIndex]){
  496. maxIndex = leftChildIndex
  497. if(this.storage[maxIndex] > this.storage[parentIndex]){
  498. [this.storage[maxIndex],storage[parentIndex]] = [this.storage[parentIndex],this.storage[maxIndex]]
  499. this.siftdown(maxIndex)
  500. }
  501. }
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508. }
  509.  
  510. arr(){
  511. return this.storage
  512. }
  513. }
  514.  
  515. const a = new Heap();
  516.  
  517. a.insert(3)
  518. a.insert(6)
  519. a.insert(9)
  520. a.insert(20)
  521. a.insert(7)
  522. a.insert(8)
  523. console.log(a.delete())
  524.  
  525. console.log(a.arr())
Add Comment
Please, Sign In to add comment