๐๐ถ๐ด-๐ข ๐ก๐ผ๐๐ฎ๐๐ถ๐ผ๐ป ๐๐ต๐ฒ๐ฎ๐ ๐ฆ๐ต๐ฒ๐ฒ๐
Big O notation is a mathematical shorthand used to describe the runtime of algorithms.
It's a fundamental concept in computer science that helps us understand how an algorithm's performance changes as the input size grows.
๐๐ถ๐ด-๐ข ๐ก๐ผ๐๐ฎ๐๐ถ๐ผ๐ป expresses the upper bound of an algorithm's runtime (in terms of space or time complexity). It tells us how the algorithm's runtime grows as the input size increases in the worst case.
Time complexity measures how long an algorithm takes to run, while space complexity measures how much memory an algorithm requires.
To analyze the Big O notation of an algorithm, we need to identify the dominant term in the runtime function.
The dominant term is the term that grows the fastest as the input size increases. All other terms can be ignored.
Examples of time complexity using Big-O notation:
๐ต ๐ข(๐ญ): ๐๐ผ๐ป๐๐๐ฎ๐ป๐ ๐๐ถ๐บ๐ฒ. The runtime of the algorithm does not depend on the input size. An example is accessing a specific element of an array.
๐ต ๐ข(๐น๐ผ๐ด ๐ป): ๐๐ผ๐ด๐ฎ๐ฟ๐ถ๐๐ต๐บ๐ถ๐ฐ ๐๐ถ๐บ๐ฒ. The runtime of the algorithm grows logarithmically with the input size. An example is using binary search to find an element in a sorted array.
๐ก ๐ข(๐ป): ๐๐ถ๐ป๐ฒ๐ฎ๐ฟ ๐๐ถ๐บ๐ฒ. The runtime of the algorithm grows linearly with the input size. An example is finding an element in an unsorted array.
๐ก ๐ข(๐ป ๐น๐ผ๐ด ๐ป): ๐๐ผ๐ด-๐น๐ถ๐ป๐ฒ๐ฎ๐ฟ ๐๐ถ๐บ๐ฒ. The runtime of the algorithm grows log-linearly with the input size. Examples include efficient sorting algorithms such as Quicksort and Heapsort.
๐ ๐ข(๐ป^๐ฎ): ๐ค๐๐ฎ๐ฑ๐ฟ๐ฎ๐๐ถ๐ฐ ๐๐ถ๐บ๐ฒ. The algorithm's runtime grows quadratically with the input size. Simple sorting algorithms, such as insertion or selection sort, are examples.
๐ด ๐ข(๐ฎ^๐ป): ๐๐
๐ฝ๐ผ๐ป๐ฒ๐ป๐๐ถ๐ฎ๐น ๐๐ถ๐บ๐ฒ. The runtime of the algorithm grows exponentially with the input size. An example is the recursive Fibonacci method.
โป๏ธ Reshare if you found this helpful.