It is a notation that determines how fast or slow an algorithm is running. This speed is not determined by seconds, but by how much the running time of an algorithm increases as the elements increase.
Big O is the relationship between time and size. Throughout the article you'll see graphs with these measures and you'll understand them better in practice. We have two types of complexity (spatial and temporal).
Temporal Complexity: Determines how long it will take to execute the algorithm proportional to the size of the input.
Spatial Complexity: Determines how much memory will be allocated to find the item we need.
example:
Tempo Constante O(1):
example:
function increment(value: number){ return ++value } function decrement(value: number){ return --value }
const fruits = ["apple", "orange", "grape", "banana"] function getItem(items: string[], index: number) { return items[index] } const item = getItem(fruits, 2) console.log(`fruit: ${item}`) // "grape"
const animes = ["one piece", "dragon ball", "naruto", "demon slayer"] function getFirstElement(items: string[]){ return items[0] }
const animes = ["one piece", "dragon ball", "naruto", "demon slayer"] function getLastElement(items: string[]){ return items[item.length - 1] } let lastElement = getLastElement(animes) console.log(`Last Element: ${lastElement}`)
Linear Time O(n):
examples:
const numbers = [0, 4, 8, 2, 37, 11, 7, 48] function getMaxValue(items: number[]) { let max = numbers[0]; for (let i=0; i <= items.length; i++){ if(items[i] > max) { max = items[i] } } return max; } let maxValue = getMaxValue(numbers) console.log(`Max Value: ${maxValue}`)
Logarithmic Time O(log n)
example:
const numbers = [0, 9, 24, 78, 54, 88, 92, 100, 21, 90] function binarySearch(nums: number[], target: number) { let left = 0; let right = nums.length - 1; while (left <= right) { let middle = Math.floor((right + left) / 2); if (nums[middle] === target) { return middle; } else if (nums[middle] < target) { left = middle + 1; } else { right = middle - 1; } } return -1; } let getTarget = binarySearch(numbers, 92) console.log(`Target: ${getTarget}`)
log2(10) = 3.4
log2(20) = 4.3
log2(40) = 5.3
Linearithmic/quasilinear time O(n log n)
function increment(value: number){ return ++value } function decrement(value: number){ return --value }
Quadratic Time O(n²)
example:
const fruits = ["apple", "orange", "grape", "banana"] function getItem(items: string[], index: number) { return items[index] } const item = getItem(fruits, 2) console.log(`fruit: ${item}`) // "grape"
Time Exponential O(2ˆn)
const animes = ["one piece", "dragon ball", "naruto", "demon slayer"] function getFirstElement(items: string[]){ return items[0] }
Factorial Time O(n!)
example:
const animes = ["one piece", "dragon ball", "naruto", "demon slayer"] function getLastElement(items: string[]){ return items[item.length - 1] } let lastElement = getLastElement(animes) console.log(`Last Element: ${lastElement}`)
The above is the detailed content of Big O Notations. For more information, please follow other related articles on the PHP Chinese website!