Time complexity measures the number of operations an algorithm performs as input size grows, focusing on the dominant term while ignoring constants and lower-order terms. Algorithms are analyzed in three cases: best case (minimum steps), average case (typical steps), and worst case (maximum steps), with worst case being most important for comparison. Common complexity classes include O(1) constant, O(log n) logarithmic, O(n) linear, O(n²) quadratic, and O(n³) cubic. For example, linear search has O(n) complexity while binary search achieves O(log n) by halving the search space each iteration.
深度探索
先修知识
- 暂无数据。
后续步骤
- 暂无数据。
深度探索
COMP2521 26T2 Week 1 Lecture 2本站添加:
How is it?
I don't know.
This is short.
Hello everyone.
Is it working?
>> No.
This one.
Hello. Now it's working. Okay. The other one not working.
Okay. Hello everyone.
Uh let's start the second topic. Before I explain about this one, do you have any question from last week? Last week? No, last session.
No. All good.
So just a quick recap.
Last session we talked about recursion which is a type of repeating technique in your programming and we see that recursion is actually kind of calling the function from inside of the function and to do recursion we actually need extra memory because every call need to be saved somewhere which is uh stack call is a part of RAM.
How big is this actually a size depends on the operate this operation system that we are using or compiler we are using or normally is it like a few megabytes because of that if you are using recursion we have to be careful that uh how what is the size of the data set and how many calls or program actually create otherwise we may see a stack overflow error because of be using all this uh stack memory that already reserved.
So today we will talk about data analysis. It's very important topic recursion and this one we will continuously repeat throughout the term because in almost every topic uh we have this uh recursion and also check the uh actually the algorithm uh efficiency using this criteria that we will discuss today.
So let's see why even we need uh to analyze an algorithm.
Uh because as I mentioned yes um Tuesday we need to do programming like a computer scientist not just put the codes. We need to see that if the code that we are actually preparing or writing how good it is and how it behave with different input size which is very important for whatever uh coding or algorithm we can check two important things. one is the time or time complexity and another is space complexity for each algorithm or data structure that you are using. So and but today we will mostly focus on the time complexity. However, we see for example in recursion also space is important. You have to take care of how much space or data structure are already using and we will see this space feature in other data structures also that we will discuss later but uh we today we will mostly focus on the time. So how we can find actually time complexity of an algorithm.
So we do this analysis because of important reasons.
First is we we want to compare different solution. As I mentioned, we may have different type of sorting or different type of search but we want to see which one is better based on the data we are using. So we will see that there are really uh very different different methods and we have to care about which one we are choosing based on our data and also we can predict the performance uh and behavior of our code when we have a large input. This is very important because for a small input there is not that much differences between different search or sorting or any other type of algorithm but when we dealing with large data that's very important that which algorithm we are using and also this analysis is this is important this is machine independent comparison we are not caring about where later we want to run this algorithm.
We just want to compare different algorithm based on their mechanism itself. Doesn't matter it will be run on multiple CPU, GPUs, on laptop or whatever.
We want to see the performance of the algorithm itself regardless of the machine that we want to use for running the code. And also finally we want to have a better uh design decision based on the uh work that you are doing or data set that we have.
Um now we see few examples that where time complexity is important and then I will explain in detail actually what is time complexity.
uh just imagine we have some V programming for a high frequency trading which things change in microsconds. So here even uh how our code is running or how fast it is can affect the um the performance or in engineer um search engines that [clears throat] we have billions of pages. So our code is very matter that how deal with all this big data in real time systems in big data processing in mobile or web apps. So these are the examples that really time is important even microsconds can change things. So this is aware that we should care about the time complexity of our algorithm and about space. We know that now we have lots of memory available but still there are cases that maybe we have a limited memory available like mobile devices or embedded systems.
uh again even we have a large memory when we are dealing with larger scale data still it can be a bottleneck so we should care about how much space our algorithm is using and also recursive algorithm that already we discussed that it needs memory and it's important that we check how much memory or program actually using otherwise we will see a stack overflow uh risk any question till Now nothing.
Okay. Now let's focus on time complexity which is the important topic. The most important things here when I'm talking about time I'm not talking about second minutes and hour. We are not talking about like time as we know generally here time means how many steps or algorithm needs because if we want to focus on the real time like how many seconds needs to run our program then it will be depend on the machine that we want to run our program it is the CPU it's a GPU it's your own u actually laptop or whatever so regardless where we want to run. We want to see how many steps or operations this algorithm needs especially when we have a large input.
Overall we call it time complexity but it's actually is not the time that we normally use is a number of steps or behavior of our algorithm when we have a large data set and in help as I mentioned we compare different algorithm and see which one is faster or need actually less steps to find what you are looking for and Um sometimes uh we have to choose that we want to have a more faster algorithm or space is more matter for us. That depends on the type of the problem that we want to work on it that we choose that uh we want to sacrifice which any of them or but in most cases time is more important. So we will try to find a solution that have the minimum steps or operation to finish the task.
Uh to learn how we can find time complexity of piece of algorithm.
Uh I just started counting. I will tell you what is exactly counting is and how you can count the number of steps of algorithm but this is not something that I ask you later for example in your final question I said count the number of steps of this algorithm I'm just showing you how we count because from counting we can find time complexity just to have some idea of which from where actually this ruler coming is coming from counting. So we will see some example of counting how we can count the number of steps exactly and then later when you become expert you don't need to count you just have a look to the code and then directly can say what is the complexity of this code. So we have a difference between counting and analysis. Contin is finding the number of steps uh exactly how many times this code actually run each line of it and um any question till now okay so let's see let us start with counting you want to I want to show a few example just uh one thing if you already download this slide I upate last night. So, please download the latest version. We're going to change it a little bit and add more uh actually examples into these slides. So, use make sure that you have the latest version.
Let me have a look if any question and then we will continue.
Okay.
Okay. Let's see first example.
Suppose we have a very small piece of code and we want to see how many steps need this code to be run.
This is a a for loop and a print inside of the loop. In counting actually we will check each and every line and each and every operation and check how many times it run if you want to run this code. So for we have a two lines the four and then print f. So suppose n is whatever number you think how many times that four loop will run n times.
>> I don't think so.
It's n + one time. Why?
End time the condition is correct and print will be run. But the last time we will check and the condition is not made. So it will stop running. Correct?
So for line itself n + one time but the print inside of it end time. Any question on this or is clear?
>> So >> yes.
>> Yeah. I want to >> because we need to know these total steps. Then we will see how from these total steps we can find the time complexity. So I just want to show you a few examples.
Let's see how can I if I can do here or I need to take it.
So this line is n + one time and this line n. So total will be 2 n + 1 the number of steps that we need to run this code.
Okay.
How about this one?
How many steps this one needs?
X² >> N² >> uh but I need to uh number of steps complete it is n² the complexity but how many steps overall sorry n² so okay let's Okay. The outer >> loop n + one time correct. How about the inner one >> again? n + one is it correct?
So what should be >> just n >> no >> sorry >> no just I'm talking about this line itself >> that we will see print f how many times will be wrong >> we know that when i is zero j will go from zero to n correct and then we back I will be one and then J again zero to N.
So again and again again. So can I say n * n + one this n coming from outer uh actually loop and n + one is this one itself because n times this loop running and how about print Yes, correct or no?
Y n multiply n.
>> Sorry.
times.
>> Yeah, because the loop always we check one more extra n + one time but whatever is inside of the loop is one time less.
So is n time itself. Since these two line are uh inside of the outer uh actually loop.
So we will multiply by n.
So overall how many steps? n + 1 + n2 + n + n2. So 2 n 2 + 2 n + 1.
This is the number of steps. Oh, sorry.
Please any question?
Yes.
>> Explain again. Okay.
So you you uh agree that the outer for running n plus one time.
Yes. So whatever is inside of that loop will run one time less. Okay. So the second loop and print f will be n but print f itself is inside of the second four also.
So based on that so this outer layer is n + one time whatever is inside because of this outer four will run n time plus this four also is here which is n + one time now which part is like not clear for you any other question yes >> why doesn't the happen as a step >> sorry >> why doesn't the happen as a step Which one I j >> we actually when we counting the detail we can count each of these stems separately but I to simplify I'm just considering loop as a one together but if you want to go in detail then int i equal to zero is different operation condition is different operation and that's uh increasing Decreasing steps also different but to simplify it I'm just considering whole loop as one because both are correct.
>> Yes.
>> I will show you.
>> Yeah.
I'm just starting with easier one and then go for more complex.
How about this one?
This is not nested loop. The previous one we call it nested loop. This is two separate loop but one program.
What is the number of steps here? I just want to make sure you understand this part because this time complexity is actually very important. If you learn now then you will be guaranteed that whole term you will not have any issue with this subject. Because of that I put different example just to show you.
Please if you have any question just ask now Okay, here we have two independent for loop.
This one how many times?
>> n + one and this one end time. And this one n + one and this one end time. Because this loop is two separate loop this not nested. So the total will be 4 n + 2 number of steps.
Now we will see example why how it works here or index for a step every time dividing by two.
So it's not like previous examples that increasing by one or decreasing by one.
You'll be dividing every time.
What do you think about this one? Any idea?
>> Yes.
>> Sorry. [clears throat] >> Which one?
>> Maybe.
If you see something like this that you are not sure, you can just try it with a small n to see how many times each line is actually running. Like just put n5 and see how many times that while and the lines inside of the line uh while is actually repeating.
Initialization is always one because we just one time initialize but whatever is important is this part the y one every time this index is dividing by two just let's show you like small example suppose n is five okay i is five here So I will be five. Here in the while loop it says if I which is five is more than one. Yes.
Then I divide by two. So five 2. So I will be two and print will be run. Next time in I is two. If two is more than one.
Yes. Again I will be 2 / by two which is 1 and print will be run. Next time I is one.
So if one is more than one, no. And then finish.
So because of the the I is every time dividing by two. So while loop itself and the lines inside of it just running actually log n times base 2.
So if I say this line is one time this one log n + one that because we see we have a extra check you should check one more time to find out this condition is uh actually correct or no and whatever is inside of the file again log n log n so it will be 3 log n + two.
Yes.
>> Again.
>> Yeah. Because every time that's uh counter or I dividing by two. So that's it's running log n base two times.
>> So it's like exponential.
>> Yes. given is I * 2 again it will be log >> so is it just log or is it log base two >> base two but mostly we ignore base >> but because it's dividing by two is actually base two if it was dividing by three will be base three >> okay >> yeah you can just draw face just put is correct Yeah, but then just put it so you know that because of the division of two and based on what you are dividing that base can change but more complicated you know the actual >> yeah actually for time complexity and just we just you will see you just have a look and then say the complexity directly you don't need to count in detail because that is the one of the problems I will mention that counting is not always possible because code can be very complicated and very difficult to count one by one but I'm just showing you counting then when you see complexity you know where it's come from but yes that's correct we cannot do for like complicated codes >> when we express you also like task to for this group.
>> Yeah, that two coming from one is from the first line and one is from y.
So that plus two coming from those two numbers. this one because this initialization just run one time and then always the y itself run one more time to check the uh condition and this is the last example then we will see how we can simplize this and don't need to all this is the last Yes.
>> Yes.
Okay, this one for itself n + one time and whatever is inside of the four will run n times. Correct? So all of this line has n already now. So this one end time. Now we have a while. We already see that while run log n + one time log n + one time. And whatever is inside of the vile is log n and again log n. So then you need to sum all this.
So how many this will be n + 1 + n + n log n + n + n log n + n log n 3 n log n + 1 2 3 n + 1 So as you can see these are the like simple code it takes like is getting more complicated. So it's not common to count the exact number of steps. What we need is to know how this code behave when we give a big data as input.
Any question here?
Okay.
Uh to simplify all this counting because we don't need all this detail. For example, just think for example this one if n is million if it's really matter this putting this one here.
No, because we always want to check complexity in the worst case when the input data is very big. So looking all these detail when we have a very big data actually is uh not helpful because that's will not have that much effect on the total complexity of the code.
So uh to simplify counting and then find what part of this actually containing uh dominate the rest we use theory we call it asic behavior. In ascent behavior we say actually an algorithm behaves when the input size become very large and also we ignore a small detail and focus on the growth trend. We have an example here. For example, if the number of counting is 2 n + 5, if n is a small like n is five, there is no that much difference between two n terms and five as a constant. But if n is large like 1 million, then that five actually um not dominant and the other part is dominant. So when we want to show the complexity of the code, we want to see which part is dominant just that is enough to say to say how uh complicated is a code.
For example, look at this examples.
Uh imagine we have some codes and we count the number of steps. Now we want to say actually what is behavior of this uh piece of code or algorithm. If we have a large input data uh when we want to simplify we always focus the larger part of this country and also we don't care about the constant numbers for example for the first one I will do one and then you can also do the rest for this one for The first one is 100 n + 12 n + n. For the large n actually what is important is n itself. So if I want to simplify we just say the behavior of this co this part of code is n base based of something uh behavior. How about this one? Which one is dominant?
>> Just this. The rest is not actually important when the size of the input u data is large. How about this one?
n log n. We just pick the largest term in this uh equation. How about this one?
This one we always drop the constant.
This one n is larger or log n.
This one n square n.
This one >> log n. This one any question?
>> Yes.
mediumsized programs would still >> No.
>> Okay. Because we always uh check the worst case. We will say that if there really is a very large data uh we will see that actually is the next slide when we were analyzing algorithm we have a base case average case and worst case but what is we really care about is the worst case but I explain why worst case is important. So here when we are simplifying we just think about what is the worst case.
Uh as I said when we analyzing algorithm uh we can check it in three different cases. The base case it means with minimum number of steps we can get the results that we are looking for. Average case in average how many steps this code needs. For example, if you are searching for something just imagine we are doing uh search and worst case what is the worst case to or the maximum number of steps a code need to get what we are looking for. Um best case and average case all of these actually depend on the input data.
uh but we always care about worst case because we don't know that regarding the input data that we have even ever base case or average case happen. So when we are comparing different algorithm we will see uh worst case and in worst case which one is better not base or average case.
And for base case normally we use the omega for average case theta notation and for worst case big O. But you can see in some uh resources for showing all three of these cases they just use big O including this course. If you see the rest of the slides whenever we talking about best case, average case or worst case we always put big O. The reason I put two other just if you see somewhere else you know that this is correct. They have a specific notation for each of these cases. But in some resources, they don't care about it and they put everywhere big O. We also will do the same. Yes.
You don't have to just define all this particular condition that are working that is itself needs lots of work to find exact number of steps and then later you will see that finding complexity is not difficult that much.
>> Yes.
Uh yes actually we are looking in the worst case when we are getting more dominant part that is what you're asking that why we get the most dominant >> isn't it the best case because you're not including all >> no best case actually for example imagine you have the data and you want to um is already sorted and you are looking for a smaller item and you are doing linear search we will not check you with first check you will find what you are looking for so just with one search you find what you are looking for so this is the best case in the worst case is just random so you have to check each and every item to find what you are looking for so if best average worst case is depend on the input data if it's sorted not sorted is random or whatever based on what is the problem so now uh so from now on I will show all of them with big notation now uh let's see two examples uh linear search and binary search which is uh clearly We can show uh difference between algorithm that doing same thing and also we can see what is the best average and worst case in any of these cases. Linear search means um we want to find a particular item in array. For example, in your search, we will search one by one from beginning until end of the array to find out if the item that you are looking for actually exists in the array or not. We call it linear search.
Uh, and here you can see the code. We start from first item until the last one. And as soon as we find the target uh this actually for loop will uh stop. And if we reach to the end of the array and we couldn't find the item has been return minus one which means that item is not exist in the array.
Just imagine what we are looking for is the first item in the array then it will be the best case because we just run this for one time and then we could find what we are looking for. So the complexity is just 01.
Uh just one thing is not because of we run it one time. We always if we run uh for a constant number like two times, five time 20 times we can do a task. We always show it with 01.
And what is the average case? If we just search almost half of the array and then we find whatever we are looking for in this case we almost just check half of them and again we say the complexity is O the last part I will explain more later and the worst case is the item that you are looking is almost the last item or is not exist so we have to check each and every item in the array to see if it is exist in the array or not. This is the worst case that we have to check all the cases.
Any question here for linear search?
Yes.
>> Yes.
Big O the notation I said in the previous we have three different type of notation to show best case, average case, worst case, omega, teta, big O but as I said in this course and maybe in some other resources they show all three of them using big O. This is a notation that we use for showing time complexity of code.
So is it clear for you here what is the best operator or worst case or no?
>> Is it? Yes.
>> So what if in addition to the if there's an if else or else? Do you count?
>> Yeah, there's an if else. Sorry, an else linear search. Now every single branch.
>> Yes.
>> Linear search just take items from first to last. So there is no other else or other stuff. But if it code has more component then we have to have a look what the algorithm is doing and how it behave regarding the input data.
But here this linear stretch is always like this.
And from the code also you can say it is O correct because you just have one for loop which is the main steps coming from four which is n + one time running. We ignore the constant. So the n is dominant here. Correct.
Yes.
>> Oh, here. No, we don't because now uh I suppose you know that uh the main line here is for loop which in this condition train n + one time and we ignore the constant. So overall it is o n in the worst case. We don't need to see all the detail. Just check the loops.
If I give you even a bigger code, you just see how many loops it has. If this is nested or they are separated and just just uh visually you can check the algorithm and find out how many times is right.
>> Yeah, you better two nested or nested and also you have to check the steps also. It is increasing uh one by one, decreasing one by one or dividing or multiplying and based on that you can say it is n or log n or whatever just have a look the main part how many loops you have if they are nested or no and how the steps changing these are mostly affect the overall complexity of your algorithm.
>> Yes. Is there anything?
>> Yes, I will show you. Yeah, >> it can be everything mathematically meaningful.
So in linear search doesn't matter the data is sorted or unsorted. will always check from first item until find the item or go to the last item and cannot find it.
Uh very simple but it's not efficient when we have a large like million of uh item in array then you have to check one by one until end. So it's not efficient when the data is quite large.
Any question about linear search?
And now we see another way of searching which is binary search. And then we can have some understanding how different algorithm actually can uh affect in final uh performance of your code.
The second method for searching item is binary search but it just work on sorted array. This is important. It should be ascending or descending sorted. We suppose in this course that if you are talking about binary search or we saying it is sorted it's sorted from uh smaller number to larger number it is ascended.
So uh to start from lower smaller number now let's see how this algorithm work and then I will show few examples so you can understand better because is already sorted when we are looking for an item first we directly have a look to the middle item we should find the middle of the array if the item that you are looking is the middle one the search will be complete and finish otherwise we will compare the target with the middle item. If the target is smaller than the middle item we need to just check the first part of the array or left part of the array and if the item that we are looking for is larger than meat we just check the right uh side of the array. So every time we divide our search space to the half instead of looping items one by one every time we divide our search space to the half. So we don't need to check each item separately to find what we are looking for.
Um this is the algorithm for binary search. Just imagine we have an array.
The first index of the array we call here low and last index high. In some resources you can see left and right the same.
Uh as long as the index of low is smaller than or equal than high. First we want uh we will find the middle of the array. If the middle item is uh the target that we are looking for then we return middle and the search will finish. Otherwise we will check that if target is less than middle item we have to search the first part of the array or left part. So we have to uh check change uh the index of the low otherwise we will search the other part of the algorithm array and this is just uh recursive algorithm but doing exactly the same uh as long as low is more than high uh if it's low more than high it means I mean the base case or uh stopping condition otherwise we will find the mid of the array and then again compare with target and if is needed we check the left part or right part of the uh actually uh array now let me show you the process using few examples then it will be more clear for you how is actually working any question here or I show you like example how it work.
Okay, let's try this.
Okay, we are looking for nine.
Now we want to see how we can do it. First we have to check if this array is sorted or not. We can see that is already sorted. So we can apply u binary search otherwise binary search will not work.
Um this is index first one two three four five and six. First thing um this is low and this is high and the condition is always low should be smaller than high otherwise that's the sub condition that you have to stop searching. So now low is because we just started low is zero, high is six. We have to calculate mid which is 0 + 6 / 2 which is three. So this is our mid item.
We are looking for nine. So definitely mid item is not nine. But we know that 9 is larger than or greater than seven. So [clears throat] with just one step we actually know that we have to just search the right section of the array. So or search space became half. We don't need to check the other part at all because we are sure that what we are looking is in the right sorry. So in the next iteration now low will be mid + one because we are checking just this one. It seems we have just this array 9 11 30. So here is low and here is high. So low is mid + one.
Mid was three. So 3 + 1 will be four and high is six. Again we calculate meat which will be five. So this one index was let's put different color. This index is four five six. Now mid is this item.
So definitely this is not nine but 9 is smaller than 11. Again we just need to check left part.
So here we are taking left part low is 4 high is mid - 1 which is 5 - 1 4. So the mid will be 4 + 4 / 2 4. So now we check item four and we found what we were looking for just in three steps instead of checking item one by one.
Any question?
>> Yes.
>> Four.
>> Yeah. Yeah. It doesn't matter because if you want >> you always check the floor when you divide.
>> Okay.
>> Yeah. So >> the even or odd >> but is there a scenario where this happens and it is >> no >> it cannot miss it.
What if the number appears twice in the >> number doesn't exist?
>> Twice. Uh normally we say no repetition but if it is twice it can find it. The the for example if there is multiple as soon as find one sample of that number the the search will finish. You will not care about is there any other more or no.
Yes.
>> Is there a way to program it so that >> yes, you you should change it. You if you find it then you shouldn't uh like return and break the loop. If you want to continue then you will actually because you finding all the possible repetition then you are actually breaking that binary search rule and using more steps than the original version is used. What is possible?
>> Okay, let's see another example.
Okay, another example again it should be sorted and we will start again uh index 0 1 2 3 4 5 6 7 8. So low is 0, high is 8, mid will be 0 + 8 / 2 which is 4.
We found the mid item which is 17 and we are looking for 23. So we need to just have a look to the right side of the array. So our search space became half.
Now we have to have a look just this far.
So low will be mid + one which is 4 + 1 5 and high is still eight. And again we have to find mid which is 5 + 8 / by 2.
um will be six.
So next me is this item this one.
But again we didn't find 23 and we know that 23 is larger than 19. So again we have to have a loop to the right part of the remaining array. So next time we just searching this part.
Low again will be mid + 1 which is 6 + 1 7.
High is 8 and mid will be which is seven.
Still what we are looking we didn't find it and we know that 23 is larger than 21. So we have to have a look again the right side of the array here. Low will be mid + 1 which is 8.
High is 8. So mid will be 8 + 8 / by 2 eight. We check and we found it. Finish.
So in four steps we could find the number that we were looking for.
Any question?
>> Yes.
>> Sorry.
>> 7 + 15 >> divide by two. [clears throat] seven and a half you always get the flow.
>> Yeah, because sometimes there is odd number, sometimes is a even number.
Always divide and get the flow.
And the last example to finish this.
Okay, this one.
Okay, this one we know that it's not exist but we have to see how actually by research find out that this item actually is not exist in the array. The array is already sorted and we are looking for item two.
Again this is 0 1 2 3 4 5 and 6.
Low is zero.
High is 6 and mid is 0 + 6 / 2 which will be 3. So this is the middle item.
Uh we didn't find two and we know that two is smaller than 19. So we just search the first half of the array. This one.
Now low is again zero and high is mid - one which is 2 and mid is 0 + 2 / 2 which is 1.
We have a loop to item one. Again we didn't find two and we know that two is smaller than six. So we have to have a look again this part here. Low is zero and high is mid - one which is 1 - 1 which is 0. So mid will be 0 + 0 2 which is 0. We have a look to the zero item.
Still we didn't find.
So we know that two is actually smaller than three. So it try to go again to the left to find the item. [clears throat] So it says low is mid sorry low is zero and high is mid minus one which is 0 - one - one. Now there is a condition here low become more than high. So this is a stopping criteria. So it means the item is not exist and the binary search will stop.
So we checking the low and high index.
If low become more than high then it will stop.
This is another example just show how it works. Always we check middle item. If we looking for something smaller than 11, we have to go to the left. If we're looking for something larger than 11, we have a look to the right. Again uh we find the if for example what we are looking is uh in the left side again we find the middle and then have a look to the left again right. So this is continue until either we find the item or we meet the stopping criteria and the search will stop.
Um best case here if uh the item the target that we are looking for actually is the middle one.
So just we finding first middle we find the item. So it will be best case an average case uh and worst case are the same and it is log n because every time we have to search a space. So because of that this is law and the difference is as I said when the input data is quite large for example if I has a million item and you are looking for item if you do if you use linear search you need million time check items one by one if you're using binary search in 20 steps you can find it. So as I said this difference between algorithm doesn't make sense when you have a large input data. Now you can see how different acting these two uh actually search.
Yes.
>> So it will not be binary search. it will be again sorted then there will be I don't know how we can call it >> so but that will be even more complex because then you have to have three searches space to have a look but it's possible to implement it that way regarding your data less steps like the number >> I'm not sure I have to see how it works in the large data but because every time you are finding two mean and more comparison probably even more steps.
>> Okay.
>> Because the number of check you are doing even more.
>> Okay. Thank you.
>> I'm not 100% sure but it is it will be more.
>> Thank you.
>> Do you need break?
>> Yeah. You can have just quick five minutes break and then we will continue the last part.
Are you ready?
Yeah.
>> Okay.
This is question.
Why is it fun for other cases big old?
Because as I said in uh some resources we always use big old to try and um best average.
>> Okay, let's uh see another example.
Um this code is finding the maximum in an array maximum number in in this array and just imagine it can be like unsorted array that we have. So what this doing is actually checking first consider the first item as the max and then check all other items and comparing with the one that already put in the max and if every time you find a larger number which replace the previous max with the one that find. So in this scenario you think what is the best average or worse uh complexity for this algorithm.
How many times you need to search to find the maximum here? Just give you some help.
Uh again you don't need to counting okay you don't need to count line by line based on the example that we saw we we see that the main number of stairs comes when you have a loop. So you just need to have a look that how many loops you have if is nested or is a separate loop.
And the second things you need to have a check the steps is it increasing decreasing dividing or multiplying. So you just give me that dominant case. You don't need to skip step by step. Yes.
>> Is it always?
>> Yes.
>> Is always N because doesn't matter is the first item, middle item or the last item. You will check all the items. So it's always O N.
This one is early black. Uh is want to search to find which item is zero. If there is a zero in this array, if it is zero, it will break and then stop searching. How about this one? What is the best average or worst case here?
Looking for zero in array.
What is one?
>> The best is one because my zero will be the is the first item in the array. The average it can be somewhere in the middle. So you check n / two half of the actually array or in worst case it can be the last one or it will not even exist.
So it will be O1 O N O N for M / 2 we again say O N we don't have O N / two because we always ignore the constant. Okay.
[clears throat] So in middle also in average also it is O N.
How about this one?
We have an S2.
What this code is actually doing it just summing the indexes. Is there any best average or worse in every cases is square n square n because it's just summing the indexes.
There is no stopping condition that we say we found it we didn't find it is go further or no. So if this code is always run square n time.
So the last part big or rules as I said we always care about the worst case when we looking at algorithm we want to find in worst case which is a large input data set how behave our algorithm or overall generally uh to find the big O we always drop constants constant we don't care about constant when we want to say complexity we always keep the dominant term.
Um if we have a sequential statement like sequential four we add this uh different complexity that we find if it is nested we multiply and we always we ignore lower terms.
This is the example. For example, if we have something 100 n, we always drop the constant. We say it's O N.
We always keep the dominant term. So here we say O² N or if we have a sequential state always we keep one of them because it will be two O N we drop always constant.
If it is nested loop uh there is a multiply between them so will be a square n and always we ignore the lower terms so we ignore log n and the final is square n now let's [singing] see oh and you ask that we have any other class of complexity except for o n or log n uh the or algorithm can have different class of complexity. If it's just run for a constant number three times, four times, five times, we always say 01. Or it can be logarithmic, linear, log linear, quadratic, cubic, sublinear, exponential, factorial. So it depends on how many times our code is running.
And this is diagram as we show how uh grow your complexity of your code constant with always less and then logarithmic linear until the worst case which is factorial which is more complex regarding the time complexity.
Now let's have few example this final section complexity.
Imagine you have this program this algorithm overall is one algorithm. Now we want to see the complexity of this algorithm. You don't need to do counting.
Just have a look to the loop and step in each loop and based on the weak or rule tell me what is the complexity of this whole code.
So what is the complexity for the upper part overall square n correct and the lower part >> log n and we always keep the dominant so I can say square n is the complexity of all code so it will be big o square n just I don't know if draw here or Where is not here?
So it's overall is like this.
So now you know all the detail you don't need to continue. But now you understand where is it come from. Correct? Any question? Is clear that where come from?
Yes.
Example five from Kanti.
>> Yeah.
>> Yes.
>> Uh n to square n for all of them.
>> Yes.
How about this example?
Can you tell me what is the complexity of this code?
How many loop you have?
You have two loops and both of them the steps are divided by two and it's a nested loop.
So what is the complexity?
log n².
Yes.
So because it is a nested so we have a square and both of them steps are dividing by two. So it's log n² this one easy.
So log is not always for y for loop also we have a loop to the step because is multiplying by two again it is log this one because there are two separate loops.
One is O N, one is log N. We always skip the dominant one. But overall the complexity of this code is O N.
This one O N.
So even it's plusing every time two steps still is O. The condition of log is apply if you are have a division or multiplication.
This one >> again log n. If you want to be very precise then base is three but you can drop it not necessarily you put it. So still it is log n because every time is multiplying by three.
This one, >> louder.
square.
>> Despite the second loop is actually based on the index coming from first loop, still it is a square.
This one >> n because the second one is just a constant this one.
So it is just O N.
In big data when n is quite big that 10 actually doesn't matter. So the overall complexity is just all n.
Last one here.
>> [clears throat] >> No.
>> No.
>> No.
When this loop is stop correct, this is the stopping condition.
is bishing things.
Yeah.
So, any question? Now you know how to check the complexity of your code. So the most important part are loops and then checking the step in your loop and based on that you say overall complexity and always draft the constant and always stick with the dominant term. Okay.
So last thing sometimes you have a multiple variable for example you have here nested loop one is until n one is until m since we don't know what number is n and m we should consider as a separate we cannot say they are almost same so here we cannot say o² n this is o n * m because we don't know actually what is n and what is m. So if you have multiple variable you keep it separate. Don't say square n here.
question.
>> Yes. What is >> that's every time like it is act like a factorial like it's looking for whatever you have to check all the previous again like for example in each check you have to do checks again from first of the algorithm like factor tutorial if you looking five you have to get one multiply 2 3 four so all the previous uh actually operation need to be repeat if you have a problem like that so in every step you have to check all the previous steps again then behave >> okay thank you >> I don't have example now here to show you how about this Yes. Big O N plus big O M. We cannot drop one of them because we don't know what is N and M. So we keep both of them.
This one N log M.
Last one this actually comparing two strings to check if they are equal or no. So what will be complexity for this piece of code? Comparing two strings character by character. As soon as find a difference between them it actually [clears throat] stop.
Sorry again.
two string and one of them's length can be n and another can be m. So when it is stop minimum so it will be o main n and m.
Suppose that length of first is n and length of second is m. As soon as the comparison one of them will reach to the end of one of these strings this loop will stop. So it's minimum of length of these two string.
So overall we see that counting give you exact number of number of steps and analysis which finding the complexity we actually show the growth of these steps and we don't care about this constants and we just keep the dominance part.
Um yes that's all for today.
Please don't forget to leave uh feedback so I can update the size based on your feedbacks.
Thank you.
>> I have a question.
>> Yeah, >> just let me stop this uh stream.
>> Sorry.
Just stop.
相关推荐
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
3D Basics in C
HirschDaniel
2K views•2026-06-05
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
Instagram accounts got PWNed
EricParker
13K views•2026-06-03
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











