Blog

How O(log n) is better than O(n) time complexity in DSA?

Share your learning

In Data structure and algorithms we heard a lot that O(log n) is better than O(n) time complexity. 

Today we will prove it with examples.

In mathematics log has base 10 where computer science has a binary system. So, the log base is 2 in computer science.

Let’s prove how logarithmic time complexity is better than linear time.

log(n) = log2(n)

If logb(n) = y               where by = n;

Example,

log(16) = 4                because 24 = 16
log(n) = y  means  2y = n

When n increases by double y just need to increase by 1. 

Here are some examples,

    24 = 16

    25 = 32

    26 = 64

So, where we need 64 iterations with O(n) time complexity, we can solve it by just 6 iterations with O(log n) time complexity.

Hence O(log n) is better than linear time complexity O(n).

Although sometimes it is not possible to solve the problem in O(log n) time then O(n) can be the next best time complexity.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago