Guest User

Untitled

a guest
Feb 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. -----------------------------
  2. EVEN OR ODD
  3. -----------------------------
  4. function isEven(value){
  5. if (value % 2 == 0){
  6. return true;
  7. }
  8. else
  9. return false;
  10. }
  11. -- the function returns one value; either it returns odd or even
  12. -- since it only does one thing (no loops) this is Constant Time 0(1)
  13. ----------------------------
  14. Are you Here?
  15. ----------------------------
  16. function areYouHere(arr1, arr2) {
  17. for (let i=0; i<arr1.length; i++) {
  18. const el1 = arr1[i];
  19. for (let j=0; j<arr2.length; j++) {
  20. const el2 = arr2[j];
  21. if (el1 === el2) return true;
  22. }
  23. }
  24. return false;
  25. }
  26. -- one loop and then a nested loop so looks like polynomial time
  27. -- Best case would be constant Time 0 (1) since it could only go through 1 loop (?)
  28. ----------------------------
  29. Doubler
  30. ----------------------------
  31.  
  32. function doubleArrayValues(array) {
  33. for (let i=0; i<array.length; i++) {
  34. array[i] *= 2;
  35. }
  36. return array;
  37. }
  38. -- 1 loop = linear time (depends on array length)
  39.  
  40. ----------------------------
  41. Naive Search
  42. ----------------------------
  43. function naiveSearch(array, item) {
  44. for (let i=0; i<array.length; i++) {
  45. if (array[i] === item) {
  46. return i;
  47. }
  48. }
  49. }
  50. - 1 loop = linear time(depends on array length)
  51. ----------------------------
  52. Creating pairs
  53. ----------------------------
  54. function createPairs(arr) {
  55. for (let i = 0; i < arr.length; i++) {
  56. for(let j = i+1; j < arr.length; j++) {
  57. console.log(arr[i] + ", " + arr[j] );
  58. }
  59. }
  60. }
  61. -- nested loop so polynomial time (n^2)
  62. ----------------------------
  63. Computing fibonaccis
  64. ----------------------------
  65. function generateFib(num) {
  66. let result = [];
  67. for (let i = 1; i <= num; i++) {
  68.  
  69. // we're adding the first item
  70. // to the result list, append the
  71. // number 0 to results
  72. if (i === 1) {
  73. result.push(0);
  74. }
  75. // ...and if it's the second item
  76. // append 1
  77. else if (i == 2) {
  78. result.push(1);
  79. }
  80.  
  81. // otherwise, sum the two previous result items, and append that value to results.
  82. else {
  83. result.push(result[i - 2] + result[i - 3]);
  84. }
  85. }
  86. // once the for loop finishes
  87. // we return `result`.
  88. return result;
  89. }
  90. -- Best case = constant Time 0(1) since (if num = 1)
  91. - worst case = linear time
  92. ----------------------------
  93. An Efficient Search
  94. ----------------------------
  95. function efficientSearch(array, item) {
  96. let minIndex = 0;
  97. let maxIndex = array.length - 1;
  98. let currentIndex;
  99. let currentElement;
  100.  
  101. while (minIndex <= maxIndex) {
  102. currentIndex = Math.floor((minIndex + maxIndex) / 2);
  103. currentElement = array[currentIndex];
  104.  
  105. if (currentElement < item) {
  106. minIndex = currentIndex + 1;
  107. }
  108. else if (currentElement > item) {
  109. maxIndex = currentIndex - 1;
  110. }
  111. else {
  112. return currentIndex;
  113. }
  114. }
  115. return -1;
  116. }
  117. -logarithimic time 0(log n) -- the looping halves each iteration
  118. ----------------------------
  119. Random Element
  120. ----------------------------
  121. function findRandomElement(arr) {
  122. return arr[Math.floor(Math.random() * arr.length)];
  123. }
  124. - constant run time complexity 0 (1) (only one action)
  125. ----------------------------
  126. Is it Prime?
  127. ----------------------------
  128. function isPrime(n) {
  129. // if n is less than 2 or a decimal, it's not prime
  130. if (n < 2 || n % 1 != 0) {
  131. return false;
  132. }
  133. // otherwise, check if `n` is divisible by any integer
  134. // between 2 and n.
  135. for (let i = 2; i < n; ++i) {
  136. if (n % i == 0) return false;
  137. }
  138. return true;
  139. }
  140. - Best case it hits the first statement = constant time 0(1)
  141. - Worse Case is linear run time complexity 0(n) -- depends on what n is
Add Comment
Please, Sign In to add comment