1.5.3 Time Complexity of While and if #3

1.5.3 Time Complexity of While and if #3

Introduction

In this video, the speaker discusses how to analyze loops in C language and determine their time complexity. They compare for-loops, while-loops, and repeat-until loops.

Loop Types

  • There are three types of loops in C language: for-loops, while-loops, and do-while loops.
  • In older languages like Pascal, for-loops were written differently than in C language.
  • While analyzing a loop's time complexity, it is important to understand when it will stop and how many times it will iterate.
  • Repeat-until loops are different from do-while loops because they repeat as long as a condition is false and stop once the condition becomes true.

Time Complexity Analysis

  • For-loops have a time complexity of order n because they execute n times.
  • While-loops can be analyzed similarly to for-loops by counting the number of iterations. Whatever function results from this analysis can be simplified to order n.

Conclusion

  • While any code that uses a while-loop can also use a for-loop (and vice versa), understanding the differences between these loop types is important for analyzing their time complexity.

Analysis of Algorithmic Time Complexity

In this section, the speaker discusses how to analyze the time complexity of algorithms using different types of loops.

While Loop vs For Loop

  • The speaker demonstrates an example of a while loop and shows how it can be used to calculate the time complexity of an algorithm.
  • The speaker rewrites the time complexity in terms of log n and explains how it can be analyzed using for loops as well.
  • The speaker explains that in C language, any initialization, condition or updation can be written in a for loop and shows examples.

More Examples

  • The speaker gives more examples of while loops and shows how they can be rewritten as for loops.
  • The speaker analyzes another example using a while loop and calculates its time complexity.
  • The speaker compares the readability of code written using while loops versus for loops.

Finding GCD Using Loops

In this section, the speaker discusses how to find the greatest common divisor (GCD) of two numbers using loops.

Algorithm Explanation

  • The speaker presents an algorithm to find GCD using a while loop.
  • The same algorithm is presented again but this time using a for loop instead.

Conclusion

  • No further conclusions were made in this section.

Minimum and Maximum Time Taken by an Algorithm

In this section, the speaker explains how to calculate the minimum and maximum time taken by an algorithm using examples.

Calculation of Minimum and Maximum Time Taken by an Algorithm

  • If two variables are equal, the algorithm will execute only once. If they are not equal, it will execute until they become equal.
  • If one variable is greater than the other, the algorithm will subtract the smaller from the larger until they become equal.
  • The maximum time taken by this algorithm is order of n (n being the number of times it executes), while the minimum time taken is order of 1 (executing only once).
  • Conditional statements in algorithms can give different amounts of time depending on their conditions.
  • An example is given where a loop executes for n times if a condition is met, otherwise it executes only once.

Conclusion

The speaker concludes that calculating minimum and maximum time taken by an algorithm depends on its structure and conditional statements. It's important to study each case individually to determine its best and worst case scenarios.

Playlists: Algorithms
Video description

Time Complexity of While and if statements PATREON : https://www.patreon.com/bePatron?u=20475192 Courses on Udemy ================ Java Programming https://www.udemy.com/course/java-se-programming/?referralCode=C71BADEAA4E7332D62B6 Data Structures using C and C++ https://www.udemy.com/course/datastructurescncpp/?referralCode=BD2EF8E61A98AB5E011D C++ Programming https://www.udemy.com/course/cpp-deep-dive/?referralCode=E4246A516919D7E84225