Advertisement
samimwebdev

BiG(0)

Jun 11th, 2022
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //which code is faster and better measured by 0(n)
  2. //Time complexity
  3. //Depends on number of operation not time, as powerful PC may take less time to run the code
  4. //space complexity
  5. //How much space it should take
  6.  
  7. //Loop Example(finding array item by looping)
  8. //Best case(omega)
  9. //Average case(theta)
  10. //worst case(0)
  11.  
  12. //number of operation based on input
  13. // function log(n) {
  14. //   for (let i = 0; i < n; i++) {
  15. //     console.log(i)
  16. //   }
  17. // }
  18.  
  19. // log(10)
  20.  
  21. //Drop constant (n+n) = 2n = o(n)
  22. // function log(n) {
  23. //   for (let i = 0; i < n; i++) {
  24. //     console.log(i)
  25. //   }
  26. //   for (let j = 0; j < n; j++) {
  27. //     console.log(j)
  28. //   }
  29. // }
  30.  
  31. //Time complexity 0(n^2) - 100 items for input number of 10
  32. // function log(n) {
  33. //   for (let i = 0; i < n; i++) {
  34. //     for (let j = 0; j < n; j++) {
  35. //       console.log(i, j)
  36. //     }
  37. //   }
  38. // }
  39.  
  40. //0n(n * n * n) - 0(n^3) -O(n^2)
  41. // function log(n) {
  42. //   for (let i = 0; i < n; i++) {
  43. //     for (let j = 0; j < n; j++) {
  44.  
  45. //       for (let k = 0; k < n; k++) {
  46. //         console.log(i, j, k)
  47. //       }
  48. //     }
  49. //   }
  50. // }
  51.  
  52. //Drop non dominant part O(n^2 + n) = O(n^2)
  53. // function log(n) {
  54. //   for (let i = 0; i < n; i++) {
  55. //     for (let j = 0; j < n; j++) {
  56. //       console.log(i, j)
  57. //     }
  58. //   }
  59.  
  60. //   for (let k = 0; k < n; k++) {
  61. //     console.log(k)
  62. //   }
  63. // }
  64.  
  65. // log(10)
  66.  
  67. //constant time O(1 + 1+ 1) = 0(1)
  68. // function addItems(n) {
  69. //   return n + n + n
  70. // }
  71.  
  72. // addItems(10)
  73.  
  74. //O(logN)
  75. //Divide and conquer
  76. //finding the element in shortest possible steps
  77. //0(1)- O(log n)-O(n)-O(nlog n)-o(n^2)
  78. //best possible shorting algorithm for mixed type of data, string(other than number)
  79.  
  80. //log2^3 = 8
  81. //log2^8 = 3
  82.  
  83. //o(a) + O(b) = O(a + b)
  84. //o(a) * O(b) = O(a * b)
  85. function log(a, b) {
  86.   for (let i = 0; i < a; i++) {
  87.     console.log(a)
  88.   }
  89.   for (let j = 0; j < b; j++) {
  90.     console.log(b)
  91.   }
  92. }
  93.  
  94. //Array gotcha
  95. //Adding and removing item from  the last of the array(O(1)) as it doesn't require indexing
  96. //Adding and removing item from beginning or middle of the array(O(n)) as it requires changing indexing
  97. //accessing by index(O(1))
  98.  
  99. //How  number of operation Increase based on the number of input - how much efficient is the data structure and algorithm is!!
  100.  
  101. //pointer(understanding) pass by reference
  102.  
  103. //loop inside loop 0(n^2)
  104. //proportional0(n)
  105. //Divide and conquer 0(log n) -sorting algorithm
  106. //constant  time 0(1)
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement