[0:05]Hi everyone, and welcome to the complete DSA series, which we are going to do today, which will be regarding arrays.
[0:25]Now, when we talk about arrays, it is basically the first data structure that we are going to study.
[0:38]So, when we talk about data structures, data structures are basically structures in our code when we are programming, which are used to store data. The coding, programming, everything we are learning, the only motive behind it is that we will be able to build real-life systems one day. And whenever we talk about real-life systems, like our websites, our apps, our software, every system is run on data. Data is the fuel for development, for coding, for programming, so we should know how to handle data. So basically, in coding, while programming, while writing C++ code, if we have to store a lot of information in some way, then we store it in the form of different different data structures. And this data can be of different categories. Some data can be linear, which can be imagined in the form of a single line. Some data can be hierarchical, meaning that kind of hierarchy is formed within that data, so different types of data are stored in different data structures. So gradually throughout the series, we are going to discover a lot of different types of data and also different types of data structures. Along with this, one more word that we will hear repeatedly is algorithms. Algorithms are basically ways to perform efficient operations on data. For example, if we want to find data from a data structure, if we want to search data inside it, then the process of searching can be called an algorithm. If we want to sort that data, meaning we want to arrange it in ascending or descending order, then we will have different algorithms for that. So similarly, the different operations that we perform on data, these operations are called our algorithms. And throughout this series, we are going to study a lot of different data structures and algorithms. So first of all, let's talk about what an array is. Whenever we need to store marks of, let's say, five students in the code, how will we store these marks with existing C++ knowledge? Obviously, we will create some variables, this will be our marks1 variable, this will be our marks2 variable, this will be our marks3 variable, and similarly we are going to create five different variables that are going to store marks of five students. But tomorrow, the situation may be that we don't have five students in our class, we have 100 students. So, to store the marks of 100 students, are we going to create 100 different variables? Now, we can create these 100 different variables, but there are two problems with this. First, it's going to be very hectic for a programmer to create 100 different variables, and secondly, it's going to be very difficult to track 100 different variables in a code. And that is why, and this number does not stop at 100. For example, we are building Instagram. Now, in Instagram, we have millions of users, so will we store each user's data in a separate variable? Absolutely not. So, to solve this problem, we have the array data structure. Array says that we can build a structure like this, which we imagine like a block of data, and in each structure of it, we can store some information.
[3:55]And this entire structure is given the name array. And this entire structure has a single name, which is a single variable that we need to track. Now, here, since I had to store the marks of five students, what did I do? I built a structure of five sizes. But tomorrow, if I have to store the marks of 100 students, then I just have to build a bigger structure, but this entire structure will not be given different variable names, this entire structure will have only one name that I need to track. So, arrays solve our problem of multiple variables. Now, whenever we talk about arrays, some specific properties come with the array. First, we can store the same type of data inside the array. If we want to make integers inside this block of code, then we will store an integer value inside each single block of code. Additionally, arrays are contiguous in memory. This thing, we will understand more when we study the pointers chapter, but basically, when arrays are created inside memory, then the data is stored together, all the data is stored together. Plus, arrays are also linear. Linear means whatever array we have, we can imagine it as a straight line structure, that is, a structure in which data is arranged one after another. Now, let's say I want to store the marks of five students. So, I can create an array, which I will call marks. I don't need to create variables like marks1, marks2, marks3. As soon as I create my marks array, its syntax is that I write integer marks of size 5. I just have to write this much to create an array. First, I have to tell what type of data each block of the array will store, so I wrote an integer value there. After that, I have to tell the name of my variable, which is going to be marks, meaning the entire array will be called marks. After that, inside square brackets, I need to convey the size of my array. So, whatever blocks I need inside the array, whatever variables I need, I will tell their size here. It's very similar to creating a normal variable, if I had to create an integer variable, I would write marks1. But here, instead of single marks1, this time I have to tell the size, the total number of blocks. And each block, this memory it occupies, it occupies memory equal to one integer. It can also store one int, it can also store one int, it can also store one int, it can also store one int. So, the entire array stores the same type of data, plus contiguous in memory means what? Contiguous in memory means, let's say, this block's address is 100. If we are looking in terms of bytes, then I know that if this block of code starts from 100, then this block will take 4 bytes of memory, then the next block of code will start from 104. Then it will again take 4 bytes of memory, then the next block of code will start from 108. This will take 4 bytes of memory, and this will start from 112. And similarly, this block is going to start from 116. So, in this way, data is stored in a continuous manner in memory, it's not that data is scattered, data is stored in a continuous manner. And linear it is because we have imagined our data in a straight line.
[7:15]So, let's create an array inside our code. To create an array, we have to tell the type of the array. Then we need to convey the array name marks, and in this, we want to store the marks of five students. If tomorrow we want to store the marks of 100 students, then we can simply replace this 5 with 100. If we want to create a double array, let's say we have 100 items, or we have 65 items, then we can store the price of each item. So, in this way, we need to create a price array whose size is going to be 65. Now, there are multiple ways to create an array. The first way is this, in which we have only given the size of the array, so this type of array will be created in memory, but there will be no elements in that array yet. If we want to define such an array that we also initialize with some values, then how will we define it? To define it, we can simply write inside our code, integer marks is equal to, and in this way, inside curly braces, we can assign our five values. So, let's say the first student's marks are 99, then 100, 54, 36, and 88.
[8:21]So, in this way, as soon as we stored the marks of all five students, 99 will be stored here, 100 will be stored here, 54, 36, and 88 will be stored here. So, this kind of array will be created in our memory. Also, when we initialize arrays with some data and we want the size of the array to be equal to this data, then we can skip the size from the left side. For example, I created an array price, and inside price, I have to store the price of only three items. One is 98.99, one is 105.67, let's make it of type double, and one is 30.00. So, in this way, as soon as we stored three items, our price array of three sizes was automatically created in memory. But when we initialize the array in this way, it is not necessary. Here, if I write 50, then the array created in memory will be of size 50, but only the initial five values would have been already defined. So, we can either give a size, or if the size is equivalent to the data on the right side, then we don't really need to give a size. So, in this way, we create our array and we can also initialize it with data. Now, whenever we have to access the data of the array, to access means whenever we want to change the data at any place, in any box of the array, or print that data, then for that, we use something called array index.
[9:47]Index means position. Each box in the array has an assigned fixed position. The positions of the array start from zero. This box is at the zeroth position, this box is at the first position, this is at the second, this is at the third, and this is at the fourth. And from here, we will understand the logic of why we started our loops from zero when we practiced for loops. Because data structures like arrays, and strings that we will study later, their position or index starts from zero. So, if we have to apply any loop on these data structures, then we need to start that loop from this index value zero.
[10:23]So, if we write marks at index zero, we put this kind of square bracket and write some index value inside it, then it means we are talking about this position. If we write marks at index 3, then it means we are talking about this position.
[10:41]The indices of the array always go from 0 to size-1. So, the size of this array is equal to 5, and the last index will be equal to 4, that is, size-1. So, let's try using the index in the code. In our marks array, we have created this marks array, in which we have five different marks. We can cout the marks of any position. Let's say we cout the marks of the zeroth position, and here we also give an endl. Along with that, we can print the marks of all positions by accessing different index values. So, we have printed them here, so all our marks have been printed. And this marks bracket index is basically, now we can treat it as a single variable. Like, let's say if I had to change the value, instead of 99, I would do 101 for marks zero, then that is also possible. So, when we print the marks, we will have 101 printed. Now, we know that for an array, the valid index always goes from 0 to size-1. So, if we try to print the value beyond the size, let's say we tried to print the value at the fifth index, then in that case, we will get a warning. The warning is that array index 5 is past the end of the array. The end of the array is at size-1, but you are trying to print the value of the size, which does not exist. Or if I write -1 here, which is smaller than zero, then in that case, we are going to get the same error, array index -1 is before the beginning of the array. So, in this way, we always have to access the values of the array within the limit. If we write an invalid index, we can get garbage values. Next, we are going to learn how to run a loop over our array. Now, when we run a loop over arrays, for example, here we printed the elements by writing five cout statements, but generally, if the size of the array is 100, we will not write 100 cout statements. So, generally, to perform operations on an array, we use loops. And how do we use loops? We run a loop that goes from index 0 to index size-1 of the array. So, for a loop from 0 to size-1, we will need this size. The size of the array is either generally already given to us in a variable called size, or we can also calculate it. How will we calculate? Let's say, if I want to calculate my size, then I do it by writing size of, so this will give us the size of the entire array. We can also verify this.
[13:06]If we have this array, then this array occupies a total of how many bytes of memory, we will get its total size. For example, this array has one box that occupies 4 bytes of memory, and we have five indices. So 4 * 5, that is, it occupies 20 bytes of memory. So, what is our size of marks, or size of array, basically, it will print 20 bytes for us. We can also verify this. If we cout the size, then in that case, we are going to get 20. So here it is 20.
[13:43]Now, this is the total size of the array, but we know that each index of the array has memory equal to an integer. So, when we divide this value by the size of int, then we get the size of the array, which is equal to 5. So, in this way, we can calculate the size of any array by writing its name, finding its size, divided by the size of its type. But generally, the size of the array is already given to us in another variable, but we can also calculate it ourselves. Now, we have to run a loop that goes from 0 to size-1. So, we can basically run a loop that starts from 0, goes up to i less than size, i++. This is a very simple loop, we have done many loops inside the pattern. And every time we want to print whatever value is at index number i inside marks, and it's end. The value of the index will first be 0, then 1, then 2, then 3, then marks of 0, marks of 1, marks of 2 will be printed for us. And if we try to print it, then all our elements will be printed in this way.
[14:41]So, we have output all the values through the loop, we can also take all the values as input in the form of a loop. So, let's say we have not initialized our array, we have only told that we have an array whose size is equal to 5. In fact, we can write this size above, and here we can write the size variable. Now, to take all the values as input, we can run a similar loop that goes from i = 0 to size. And here, we can cin marks of i. Marks of i, marks of index, is basically now a variable, which can be input, output, changed. So, the five marks values that we enter, they will be stored inside our array, and they will be printed for us. Let's save and run it.
[15:27]Let's say the value we entered is 12, 13, 14, 15, and 16. We entered these five values, so these five values have been printed for us.
[15:37]So, in this way, we can run a loop on an array and perform our required operations on each index of the array. Next, we will solve a small problem using loops based on arrays. We have to find out the smallest and the largest number in an array. For example, initially, we will only find the smallest number. Let's say we have this array, which is given, in which there is 5, we have 15, we have 22, we have 1, we have -15, and we have 24. So, in this way, we have different values, and from these, we have to find the smallest value. Now, to find the smallest value, we know that we will need to check every single value of the array. So, why not run a loop to check every single value? And where does that loop start? Array loops generally start from 0 and go up to size-1. So, first we will go to this index, here we will check if it is the smallest value. Then we will go to the next, we will check if this is the smallest value for us. Then we will go to the next index and keep checking the same thing until we reach the last index of the array. Now, whenever we have to check if any value is the smallest value, we have to compare it with some value. So, what we can do is, we can create a variable, int smallest, and initialize this variable with plus infinity. Plus infinity means the largest possible value. If I initialize it with the largest possible integer value, then any value, when compared with this value, will be like 5 and plus infinity. We will compare them. So, which one will be smaller? Obviously 5 will be smaller, so 5 will come in the answer. When 1 is compared with 5 later, when 5 comes here, 1 is compared with 5, then again 1 will be smaller than 5, so 1 will come in the answer here. So, in this way, where do the comparisons start from? Whenever we have to find the smallest, the comparisons start from the largest possible number so that this number always loses every comparison. Because we never want this to be the answer, we want another valid answer to come out. So, plus infinity in programming, in C++, we call integer_max. We write this entire thing in uppercase, and it means plus infinity in C++. Plus infinity means the largest possible value for any integer number, that's what we call plus infinity. So, we started with int max. Now, first we will go to the first index, compare 5. Now, when we compare 5 with plus infinity, obviously 5 will be smaller. And how will we know this? Either we can use an if-else, that is, if whatever numbers we have, let's call this array nums, if the value of nums of i at any index, if the value of nums of i is smaller than smallest, that is, whatever our answer is, then what will we do? We will update smallest to nums of i.
[18:31]I hope we have understood this logic, that as soon as we get a value smaller than plus infinity, then what will come in smallest, that smaller value will come in smallest. Now, next time, when we update, then 15 will be compared with this 5, because now we have 5 in smallest, and nums of 5 is 15. So, 15 is not less than 5, so no operation will be performed. Now, we will update our index again, we will compare 22 with 5, no operation will be performed. Then we will compare 1 with 5. So, 1 is smaller than 5. So, here, removing 5, we will write 1 in the answer, because this condition will work. After that, we will increase the index again, -15. -15 is also smaller than 1. So, here we will have -15. Then again, we will increase the index, because we need to check till the end, because we don't know that if there was -24 at the last index, then that would be the answer. So, we check till the very last index whenever we have to find the smallest or largest. Now, when we compare 24 with -15, -15 is smaller, so it will remain the same, and the final answer will be in this smallest variable, which is going to be equal to -15. So, this entire logic, let's convert it into code once. First of all, let's create an array called nums. Inside nums, we have different numbers stored, 5, 15, 22, 1, -15, and 24. These numbers are stored with us. Let's also write its size together, size is going to be equal to 6. Now, if we have to find the smallest, then we will store it somewhere inside a variable, so we initialize smallest with int max, that is, positive infinity. So now, we will run a loop, for integer i equal to 0, i less than size, i++. Every time we will compare, if the value of nums of i is less than our smallest, that is, whatever is smaller between the two, if smallest is smaller, then we don't need to change anything. But if nums of i becomes smaller, then the value of smallest will be updated to nums of i. And finally, after the loop finishes, we can print smallest is equal to the smallest value and end of line. If we run it, then our answer will be -15. If here, instead of -15, it was -24, then in that case, our answer would have been -24. So, this is how we calculate the smallest value with the help of a loop inside an array. And not just smallest, we can find largest, we can find sum of all values, product of all values. Basically, any operation in which we are required to go to each index and compute it, for that, we can use our loops in this way. Also, one more thing that I would like to teach you here is the min function. Basically, we have two functions in C++, called min and max. Whenever we have to find the minimum of two numbers, then for that, we can use min.
[21:34]This is an implicit function. It is already available with us, its logic is already written. This is a min function, inside which we pass two arguments, nums of i and smallest, and this will return us the value which is smaller. And whatever is the smaller value, we can write it inside our smallest. So, this entire if we have written, its shorter way to write it is this single statement, in which we can use our min function, and it will return us the minimum of these two values, which we will store inside smallest. So, this way is also going to return us the same answer.
[22:07]So, this was the logic for finding the smallest value. If we have to find the largest value, then we just have to reverse everything. Basically, this time we will take a variable, int largest. Just like we initialized smallest with plus infinity, this time we will initialize our largest with minus infinity.
[22:30]Because we want that largest, whatever number it is compared with, the other number should always win. So, we will make largest the smallest possible value so that it never wins when comparisons are happening. And minus infinity is int_min in C++. So, here we are going to write int min. Now, int min, we will compare it, so here, when we are doing comparisons in the for loop, along with smallest, we can find largest as max of nums of i and largest. And finally, after the loop finishes, we can print smallest and largest both. So, basically, to find largest, I don't need to run a separate loop, I can find both values inside a single loop. Because eventually, whether we have to find smallest or largest, we are going to each index, right? So, for this particular array, the smallest value is -24, the largest value is 22. So, in this way, we can find smallest and largest values. Here, pausing, you have to solve a homework problem. Basically, what you have to do is, you have to print the index at which the smallest and largest values are stored.
[23:43]So, this is one thing that you have to do, that this time, not the value, like if -15 is the smallest, then I need the index of -15, which is equal to 4. So, I have to print this 4, I don't have to print -15. How will we print this? How will we initialize the values for it? What comparisons will happen? You have to think about it yourself, so I am giving this thing to you as a homework problem. Next, what we are going to study, we call it pass by reference. Now, we had already seen pass by value. Inside the functions chapter, we studied pass by value, and pass by value means whatever primitive data type variables we have, if we pass them to a function as arguments, let's say this is my main function, inside which I created a variable x, inside which the value 10 is stored, and if the main function calls another function, which is change x, then a copy of this x will be created inside it, which I will highlight with a different color, and the same value will be copied inside it. Now, if we make any change here, if we change 10 to 15, then that change will not be reflected inside the main function. This was pass by value. Now, we are going to study its opposite, which is pass by reference. Reference means, in a way, an address. And addresses, this entire concept of reference, we will understand it in more detail in the pointers chapter. So, when we talk about pass by reference, what do we exactly do? In pass by reference, our entire data structure, we use it for non-primitive data types, like an array. Primitive means simple. Arrays are not that simple in memory, so we call them non-primitive. So, whenever we do pass by reference, in a way, we pass the address of our entire data structure to any function. And whenever you have the address of anything in programming, it means you can go into it and make changes inside the same original copy. For example, our arrays, whenever we pass our array to any function, then the array will always be pass by reference, implicitly, automatically. Let's say we take an example of a simple array, integer array of size 3, in which the values are 1, 2, 3. Now, if we create a function, let's call it void change array. Inside change array, we will have an integer array and the size of that integer array, and what will we do inside this function? For i equal to 0, i less than size, i++, we will double the value at each index of our array. We will make it 2 into array of i. So, from here, we will call the function, our change, change array, inside which we will pass our array and the size as 3. And here, we will run a loop of this kind, which will print the values of the array. So, here we will cout in main, and here we will cout our array of i with some space, and here we will cout end of line.
[26:56]So, when we run this code, we will see an interesting thing that the original elements of the array were 1, 2, 3. Then change array was called. Change array doubled every element. Now, the values will be doubled in this function, but as soon as we come back inside the main function, if it was pass by value, what would have happened? Our original values would have remained 1, 2, 3. But here, the values have been updated to 2, 4, and 6, meaning the change that happened inside change array function, that also reflects inside the main function. And this is called pass by reference, because internally what has happened is that when we created our array, when we created our array in memory, then an array of three sizes was created like this, in which 1, 2, 3 were stored. Now, this array, let's say, its address is 100, 104, 108. This kind of address we have. So, an interesting thing that happens in C++ is that the name of the array is implicitly a pointer. What is a pointer, we will learn later, but basically, a pointer is something that stores the address. Address means this array, that is, the name of my array, this basically stores this 100 value. It stores the starting address of its entire array. And whenever you get the starting address of an array, if you get 100, and you know it's an integer array, then by adding 4 to it, you will get 104, the next index. By adding 4 to it, you will get 108, the next index. By adding 4 to it, you will get 112, the next index. So, whenever we get the starting index of an array, we can traverse the entire array if we also get the type of the array and the size of the array. So, here, who stores the starting address? The name of the array, that is, the variable of the array, stores the starting address. So, whenever we make any change inside the change array function, we are going to make changes at this address.
[29:14]So, this address is where? This address takes us back to the original array. So, pass by reference means we are sending a reference, meaning we are sending this address, which is the starting address of this array. So, here, when 1 is replaced by 2, this 1, which we have replaced by 2, this will be reflected in the original, here also it will be reflected in the original, here also it will be reflected in the original. So, all the changes are reflected inside the original array, because no copy is created, unlike pass by value. So, C++, reference means address, or pass by reference means that we are making all the changes inside the original. So, whenever we pass our array to any function, we should pass it after thinking carefully, because if that function makes any changes, then they will also be reflected inside our main function or calling function. I hope we have understood the concept of pass by reference. We will study pass by reference, addresses, and pointers in more detail in our pointers chapter. Next, the concept we are going to talk about is called linear search. Linear search is a kind of algorithm. It's a fixed algorithm, the way to do linear search is also fixed. For example, we have this array. Let's visualize this array as well, this array will look something like this in memory, in which we have different elements, 8, 1, 2, and 5. So, in this way, we have different elements, and here, from here our array is starting. Let's also write its index values, 0, 1, 2, 3, 4, 5, and 6. So, the size of the array is equal to 7. Now, if we are given an array of this type, along with that, we are given a target that I have to search for this target value inside this array, and at whatever index I find that value, I have to return that index.
[31:14]But let's say if it was 80 instead of 8 here, now 80 does not exist anywhere in this entire array, then in that case, I want to return -1. -1 means invalid index, because -1 is not an index. If we do from 0 to 6, then it will be a valid index. -1 means invalid. It could also be -2, it could also be -5. If the value is found, then return the index, otherwise return -1. So, basically, I have to search for this target value in my entire array. Now, there are multiple ways to search, but out of these, one algorithm that we use to search is called linear search algorithm. Linear search algorithm is very simple. Linear search algorithm says that what method should you adopt for searching? Run a loop, and in the loop, repeatedly go to the index and check if my target exists at this index. It doesn't exist. Now, check the next index, does my target exist here? Does my target exist here? Does my target exist here? And as soon as the target is found, return its value. We were searching for 8, we found 8, so we have to return its value. That is, if it is at index 3, then we will return the value 3. But let's say we had to search for 80. It doesn't exist here, it doesn't exist here, it doesn't exist here, it doesn't exist anywhere in the entire array. As soon as we reach the last index and find out that it does not exist, then in that case, what do we have to return? In that case, we have to return -1. So, what will be its overall flow? What will be our logic? So that we can perform linear search, it's going to be a very simple logic.
[32:48]In fact, linear search, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[33:23]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here.
[33:49]I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[34:20]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at. Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[35:40]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[36:23]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[37:01]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[37:44]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[38:23]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[39:06]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[39:45]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[40:28]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[41:06]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[41:49]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[42:27]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[43:10]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[43:47]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[44:30]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[45:07]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[45:50]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[46:27]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop. Now, every time I have to check, wherever my array of i is, does my target exist here? If my array of i is equal to my target, then it means I have found a match here. Then what simply needs to be done is to return the value of the index I am at.
[47:10]Like, for example, if I am on this index, my target has matched here, then I have to return the value 3. And what is the index? The index is equal to i. So, I can simply return i from here. I am going to use a function here because it's going to be simpler. So, from here, I returned i. If this does not match, then what do I have to do in that case? In that case, I don't have to do anything, I have to move forward. So, the loop will automatically move me forward, and I don't need to write any more code. But if I keep moving forward, forward, forward, forward, and I reach the last index, and I still don't find my target 80, then in that case, I know I have to return -1.
[47:47]So, what will be its overall flow? What will be our logic so that we can perform linear search? It's going to be a very simple logic. In fact, linear search is one of the most simple algorithms. It's the easiest algorithm in programming, linear search. We just have to run a loop, for integer i equal to 0, i less than size, i++. We have to run a loop.



