Comparing complexities

We can create a table with some values to exemplify the cost of the algorithm given its input size as follows:

Input Size (n) O(1) O(log (n)) O(n) O(n log(n)) O(n2) O(2n)
10 1 1 10 10 100 1,024
20 1 1.30 20 26.02 400 1,048,576
50 1 1.69 50 84.94 2,500 Very big number
100 1 2 100 200 10,000 Very big number
500 1 2.69 500 1,349.48 250,000 Very big number
1,000 1 3 1,000 3,000 1,000000 Very big number
10,000 1 4 10,000 40,000 10,000,0000 Very big number

 

We can draw a chart based on the information presented in the preceding table to display the cost of different big O notation complexities as follows:

The preceding chart was also plotted using JavaScript. You can find its source code in the examples/chapter15 ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.