Time complexity.

Time complexity.

Imagine a classroom of 100 students in which you gave your pen to one person. You have to find that pen without knowing to whom you gave it.


Let's break down the explanation of time complexity using the given scenario:

1. O(n^2):

- This scenario involves checking each student in the classroom one by one to see if they have the pen.

- You start by asking the first student if they have the pen. Then you ask the second student, then the third, and so on until you have asked all 100 students.

- Each student is checked against all other students in the class, resulting in a total of 100 * 100 = 10,000 checks.

- This is represented as O(n^2) because the number of checks grows quadratically with the number of students (n), where n is 100 in this case.

2. O(n):

- In this scenario, you still ask each student individually, but you don't repeat the process for every student once you find the pen.

- You start by asking the first student if they have the pen. If not, you move on to the second student, then the third, and so on until you find the pen.

- The maximum number of checks required would be 100, which is directly proportional to the number of students (n).

- This is represented as O(n) because the number of checks grows linearly with the number of students.

3. O(log n):

- Here, you employ a binary search strategy to find the pen.

- You divide the class into two equal halves and ask if the pen is in the left or right half.

- Then, you continue dividing the remaining group into halves and asking until you narrow down to a single student who has the pen.

- This approach significantly reduces the number of checks required, as each division eliminates half of the remaining students.

- With 100 students, you would require a maximum of 7 divisions to find the pen (log2(100) ≈ 6.64).

- This is represented as O(log n) because the number of checks grows logarithmically with the number of students.

In summary, time complexity helps us understand how the time required to perform a task grows as the input size increases. It allows us to analyze and compare different algorithms based on their efficiency in handling larger datasets.

要查看或添加评论,请登录

Towhidul Islam的更多文章

  • Difference ways to create an Object following OOP:

    Difference ways to create an Object following OOP:

    In JavaScript, object creation in Object-Oriented Programming (OOP) can be in several ways. Here are some common…

  • Types of HTTP Status code.

    Types of HTTP Status code.

    In server responses, HTTP status codes are used to indicate the outcome of a client's request to the server. These…

  • Setting Up Nginx for Your Domain on Ubuntu

    Setting Up Nginx for Your Domain on Ubuntu

    Setting Up Nginx for Your Domain on Ubuntu This guide will walk you through the process of installing Nginx on an…

  • JavaScript Closures: A Detailed Explanation

    JavaScript Closures: A Detailed Explanation

    What is a Closure? A closure is a feature in JavaScript where an inner function has access to its own scope, the outer…

  • Recursion

    Recursion

    Recursion Recursion is the technique of making a function call itself. This technique provides a way to break…

  • Virtualization.

    Virtualization.

    Virtualization is a technology that enables the creation of virtual instances of physical hardware, operating systems…

  • ls -l

    ls -l

    towhidul@towhidul-Aspire-V5-471G:~$ ls -l total 36 drwxr-xr-x 2 towhidul towhidul 4096 Feb 25 16:59 Desktop drwxr-xr-x…

  • Linux Command Line Basics

    Linux Command Line Basics

    Lab Environment Setup One can use an online bash interpreter to run all the commands that are provided as examples in…

  • React Component

    React Component

    React components are an integral part of building user interfaces with React, and they indeed make developers' lives…

  • Web API: Now everything will be easy!

    Web API: Now everything will be easy!

    Today I will discuss with you an interesting topic. That is Web API.

社区洞察

其他会员也浏览了