Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //which code is faster and better measured by 0(n)
- //Time complexity
- //Depends on number of operation not time, as powerful PC may take less time to run the code
- //space complexity
- //How much space it should take
- //Loop Example(finding array item by looping)
- //Best case(omega)
- //Average case(theta)
- //worst case(0)
- //number of operation based on input
- // function log(n) {
- // for (let i = 0; i < n; i++) {
- // console.log(i)
- // }
- // }
- // log(10)
- //Drop constant (n+n) = 2n = o(n)
- // function log(n) {
- // for (let i = 0; i < n; i++) {
- // console.log(i)
- // }
- // for (let j = 0; j < n; j++) {
- // console.log(j)
- // }
- // }
- //Time complexity 0(n^2) - 100 items for input number of 10
- // function log(n) {
- // for (let i = 0; i < n; i++) {
- // for (let j = 0; j < n; j++) {
- // console.log(i, j)
- // }
- // }
- // }
- //0n(n * n * n) - 0(n^3) -O(n^2)
- // function log(n) {
- // for (let i = 0; i < n; i++) {
- // for (let j = 0; j < n; j++) {
- // for (let k = 0; k < n; k++) {
- // console.log(i, j, k)
- // }
- // }
- // }
- // }
- //Drop non dominant part O(n^2 + n) = O(n^2)
- // function log(n) {
- // for (let i = 0; i < n; i++) {
- // for (let j = 0; j < n; j++) {
- // console.log(i, j)
- // }
- // }
- // for (let k = 0; k < n; k++) {
- // console.log(k)
- // }
- // }
- // log(10)
- //constant time O(1 + 1+ 1) = 0(1)
- // function addItems(n) {
- // return n + n + n
- // }
- // addItems(10)
- //O(logN)
- //Divide and conquer
- //finding the element in shortest possible steps
- //0(1)- O(log n)-O(n)-O(nlog n)-o(n^2)
- //best possible shorting algorithm for mixed type of data, string(other than number)
- //log2^3 = 8
- //log2^8 = 3
- //o(a) + O(b) = O(a + b)
- //o(a) * O(b) = O(a * b)
- function log(a, b) {
- for (let i = 0; i < a; i++) {
- console.log(a)
- }
- for (let j = 0; j < b; j++) {
- console.log(b)
- }
- }
- //Array gotcha
- //Adding and removing item from the last of the array(O(1)) as it doesn't require indexing
- //Adding and removing item from beginning or middle of the array(O(n)) as it requires changing indexing
- //accessing by index(O(1))
- //How number of operation Increase based on the number of input - how much efficient is the data structure and algorithm is!!
- //pointer(understanding) pass by reference
- //loop inside loop 0(n^2)
- //proportional0(n)
- //Divide and conquer 0(log n) -sorting algorithm
- //constant time 0(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement