Data Structures Introduction
Transcript
Hello, everyone, and welcome to an introduction to data structures. My name is Steven, and over the next three hours, we'll be tackling the topic of data structures in relation to computer science. More specifically, we'll be talking about what they are going over some different types of data structures, and discussing how we can use each of them effectively with examples. This course will be a general overview of data structures, and so we won't be confining ourselves to one specific programming language. However, this series will require you to have a basic understanding of programming. If you are completely new to computer science, I would recommend our introduction to programming series, which will be linked in the description below. That video will give you all the information you need to traverse these topics comfortably.
Now, we're almost ready to hop into things. However, there are a few housekeeping items that I want to go over beforehand for those wishing to enhance their learning experience. If you're not interested in that and want to skip right to the content, go to the time shown on your screen now. For those of you staying though there are just a few things I would like to mention. Firstly, in the description, you will find timestamps for each major topic covered in this video, as well as timestamps taking you to every smaller section contained within those topics. So please feel free to skip around if you're already comfortable with one topic or only interested in a certain data structure. Next, we've decided to include the script and visuals used for the series also in the description below. That way you can follow along if you like or simply read the script if my soothing voice isn't your style.
Additionally, for each segment, I'll be including the references and research materials used to write the script for each topic. That way, if you ever feel as if I haven't explained a topic well enough, or we're just simply like more information about a certain topic, you'll have a readily made list of articles and websites, which you can use to supplement this series. If you feel like you have any questions throughout the series about maybe something I said or a certain visual, please ask your questions in the comments below. I'll try to be as responsive as possible for the first few weeks on questions you guys may have about the series and such.
And finally, in terms of housekeeping, I just like to say if you're not already subscribed to our channel, no pointer exception, then consider doing so if you enjoy this type of content. as me and my co host Sean regularly post videos in this style. We're trying to hit 1000 subscribers before the fall semester of college begins. So check us out if you end up enjoying the video. With my shameless plug out of the way, we're finally ready to tackle the topic of data structures. In this introduction segment, we'll simply cover a general overview of what data structures are, and then go over what type of information will be covered throughout the duration of the series.
So the obvious question to start with is what exactly is a data structure? Well, in computer science, a data structure is a way to store, organize and manage information or data in a way that allows you the programmer to easily access or modify the values within them. Essentially, it's a way for us to store a set of related information that we can use for our programs. data structures, and the algorithms used to interact, modify, and search through them provide the backbone for many of the programs that you'll end up writing, I can almost guarantee that in 99% of your programs data structure will be involved.
Each of the data structures that I'll be talking about are designed for the sole purpose of storing information and allowing the end user to access and manipulate that information in an efficient, effective way. But each one differs in the manner that they accomplish this. If you have a basic understanding of programming, you probably know about a few different data structures already, such as the array and the array list, also known as the list in Python. But if you're going to be pursuing a career in computer science, just knowing these two is definitely not going to cut it. Well basic data structures such as the array are used frequently by companies throughout their code. More advanced data structures are being put to use all around you the Undo redo button in Google Docs, Google Maps on your phone, even the autocomplete feature through your text messages all require the use of more advanced data structures, which makes them extremely useful to learn and comprehend.
Okay, now, I can't see you per se, but I can just tell that you're jumping up and down in your seat hooked on data structures and how useful they can be. But before we jump too far into things, let's discuss the information that we'll be covering in this series. Now, before we talk about any specific data structures, we're going to have a brief talk about efficiency. We'll discuss the metrics used to judge the speed and efficiency of different data structures, which will then give you a better understanding of why one data structure might be used over another one.
From there, we'll start by diving headfirst into what I believe are the basic data structures, those being array. An array lists. While you may already have a good understanding of what these are, I would highly suggest that you still watch these segments, because we'll be going into a little bit more depth as to why they're so useful based on differences in how they're stored in the computer's memory. After that, we'll move on to what I'll call the intermediate data structures. These are a little bit more complicated than the basics and have a few special attributes which make them stand out from the rest. We'll begin by taking a look at stacks which are the backbone for all recursive processes in your computer. Then we'll look at the antithesis of a stack the queue.
Moving on from there, we'll be covering linked lists and their evolved form in the doubly linked list. Before moving on to the final of our intermediate data structures, the dictionary which includes a mini lesson on hash tables. Then we'll wrap up the series talking about trees and tree based data structures, less linear and more abstract data structures beginning with the tree itself. We'll then move on to tries a very useful data structure used for a lot of word processing algorithms. And finally end off the series with a discussion on heaps and graphs. So in total, I'll be taking you through four different segments containing 12 of the most common data structures that are practically used,
as well as providing examples of where and when to use them. section one on efficiency, section two on basic data structures, section three on intermediate data structures, and wrapping it up with section four on tree based data structures. With this knowledge, you'll be able to take charge of your programming career and hopefully gain a competitive edge by implementing them when needed. Like I said, however, before we jump straight into the different ways to store information, I'd like to have a quick discussion on how we score the efficiency of these data structures using what is known as Big O notation. So let's jump right in.
Okay, so before we talk about all these crazy data structures, like maps, and heaps, we want a quantifiable way to measure how efficient a certain data structure is at the different tasks, we might ask a bit. If we're going to be storing extremely large amounts of data, being able to search through, modify, or access the information within a data structure needs to be fast and efficient. As we briefly mentioned before, the industry standard for this kind of implementation is big O notation. So what exactly is big O notation? And how do we measure it for a specific data structure? Well, that's what we'll be covering in this segment. For most of the basic and intermediate data structures in this series, we're going to be spending some time talking about its efficiency using big O notation. So this is definitely a topic you're not going to want
to skip. With that being said, let's begin. So because there are so many different ways to store information, like we talked about in the previous segment, programmers have developed this idea of big O notation as a way to basically score a data structure based on a few different criteria. This criteria can change based on who you ask, but for the purposes of this video, we will be using four criteria representing the most common functions you might want from a data structure. The ability to access a specific element within the data structure, search for a particular element within the data structure, insert an element into the data structure and remove an element from the data structure.
Now by measuring how efficiently a certain data structure can do these four things, we can basically create a report card of sorts, which measures how efficient a certain data structure is. At a glance, this gives us a pretty good overview of what a certain data structure is good at, and what it is bad at. And it can help us decide which one to use. If we need to store data that is easily accessible to the end user. For example, we might choose a data structure which can access elements the quickest, and vice versa, if accessing elements isn't the most important thing to us, but we need a data structure which can be easily added to and deleted from, we would go for one which is most efficient and that specific functionality.
By looking at a data structures report card, if you will, we can get a quick sneak peek at what they're good at and what they're bad at. Now, if we use big O notation to basically create a report card, like I said, there must be some way to actually grade each of these functions. And there is the four criteria mentioned, accessing searching, inserting and deleting are all scored using big O notation time complexity equations. Now what is the big O notation time complexity equation? Well, I'm glad you asked. Besides being
in an annoyingly long phrase, a big O notation, time complexity equation, or just time complexity equations, work by inserting the size of the data set as an integer n and returning the number of operations that need to be done. indicated by the computer before the function can finish. The integer n is simply the size or amount of elements contained within the data set. So for example, if we have an array, one of the data structures we'll get into soon, with the size of 10, we would place 10 into the different efficiency equations for accessing searching, inserting and deleting that represent the array, and returned back to us would be the number of operations that need to be conducted by the computer. Before completion of that function. It does get a little bit more complicated than this. But for the sake of keeping this series simple, all you need to know is that these equations help represent efficiency amongst different data structures.
Also, an important thing to note here is that we always use the worst case scenario when judging these data structures. This is because we always want to prepare for the worst and know which data structures are going to be able to perform under the worst conditions. You'll see what this means in greater detail a little later on once we start putting this into practice. But for now, just keep in mind that when judging data structures, we always use the worst case scenario. Moving on, the reason that it's called Big O notation is because the syntax for these particular equations includes a big O, and then a set of parentheses. inside these parentheses will include some function, which will correctly return the number of operations needed to be run by the computer. So for example, let's say we have a fake function, it can really be anything, the purpose in this case is irrelevant. Now for this fake function, let's say it's time complexity equation was the one shown on your screen now,
we would pronounce this time complexity equation as o of two, meaning it takes two operations from the computer before our make believe function can finish. If the time complexity equation was o with a five in the parentheses, instead, it would be o of five, and so on. Now, these are examples of time complexity equations, which run in constant time, meaning no matter the size of our data set, it will always take the same number of instructions to run. This is usually not going to be the case though, when it comes to the four criteria we want to mention for our data structures. Most of the time are integer n, which again contains the amount of elements within the data set is going to have some adverse effect on how many operations it takes to say search through our data structure, which makes sense, a larger data set means it's going to take more operations from the computer to search through that entire data set.
Now we score these four functionalities in number of operations performed, because measuring by how long the function takes to run would be silly. If we measured by a metrics such as time taken to completion, our results would be highly biased by the hardware used to run the function. a supercomputer used by Google is obviously going to be able to search through a data structure much faster than a laptop. time complexity equations essentially level the playing field by instead returning the number of operations to eliminate the bias in processing power that exists.
So to sum up everything that we've learned so far, we measure the efficiency or speed of a data structure based on how well it can perform four basic tasks accessing, searching for inserting and deleting elements within itself. Each of these criteria is modeled by an equation which takes in the size of the data structure in number of elements and, and returns back the number of operations needed to be performed by the computer to complete that task. By measuring these four metrics, we can get a pretty good understanding of what the data structure is good at, and what the data structure is bad.
Now, it's important to note that this isn't the end all be all for deciding on which data structure to use in your program. As you'll see, as this video progresses, many of the data structures were structured with specific quirks or features, which separate them from the rest and provide additional functionality. Big O notation is incredibly useful, and something that you should definitely take into consideration when determining which data structure to implement into your program, but it should not be the only thing that you use. Cool. Now that we know a little bit about how we measure the efficiency of a data structure using time complexity equations, along with the four criteria actually used to measure them. Let's dive straight into what the actual equations mean in terms of efficiency. That way, when we start grading data structures, you'll have a good idea as to how good or bad each one is at that particular metric.
Basically, we're going to cover the six most common time complexity equations from most efficient to least efficient. Now, the absolute best that a data structure can score on each criteria is a time complexity equation of o of one. This essentially means that no matter what the size of your data set is, the task will be completed in a single step. If your data set has one element, that computer will finish the task in one step. If your data set has 100 elements, one step 1 million elements one step 800 quadrillion elements, it does not matter, the computer will finish the task in a single step.
This is why when we look at the graph of volume of data versus instructions required for o of one, the line remains constant at one. No matter the volume of data stored within the data structure, that computer can complete that task in a single instruction. Whether it be accessing searching, inserting, or deleting, O of one is the gold standard absolute best top of its class time complexity equation. It is basically the Michael Jordan of big O notation when it comes to time complexity equations. Now the next fastest type of time complexity equation is O log n. While not as fast as instantaneous time, a function having a time complexity of o log n will still provide you with very fast completion. Now if you don't fully understand logarithms entirely, just know that this efficiency is slower than instantaneous time of one and faster than the next level of efficiency known as O
of n. Additionally, because the volume of data versus time graph follows a logarithmic curve, the larger the data set that you use, the more bang for your buck that you're going to get. Basically, as the amount of data you try to perform one of our four criteria on increases the rate of change of the amount of operations it takes to complete that certain task decreases. So as you can see, at the beginning of the graph here, when we increase the volume of data, our number of operations skyrockets. But when we do the same for larger and larger sets of data, our number of operations increases much more slowly than before.
A good example of a non data structures related function, which has a time complexity of o log n is the binary search. If you know what a binary search is, then you'll understand how it is that when the data set gets larger, the number of instructions needed to find the item you're searching for, doesn't increase at the same rate. If you don't know what a binary searches, but would like to know, in order to better understand Oh login. A link in the description below will take you to that point in our introduction to programming series where you can learn about it. If not, let's move on to the next equation.
O of n is the next common time complexity equation that's going to come up frequently during this lecture. The graph of volume of data versus instructions needed for this function is linear, meaning that for every element you add to the data set, the amount of instructions needed to complete that function will increase by the same amount. So to perform a function with the time complexity of O of n on a data set with 10 elements, it will take 10 instructions 50 elements will take 50 instructions, 1000 elements, 1000 instructions, and so on O of n is really the last good time complexity equation that exists. Anything above this is considered inefficient and not very practical when it comes to data structures in computer science.
The next type of equation that will come up is o n log n. This equation is the first that is relatively bad in terms of efficiency. The graph of volume versus operations shows a somewhat linear but increasing graph meaning unlike o log n, it won't be better in terms of efficiency as the size of the data set increases. Instead, the slope actually increases as the volume of data does. The last two types of equation are o n squared and o two to the N. These are both incredibly inefficient equations, which should be avoided if at all possible.
Because they're both exponential in structure, which can be seen from their graphs of volume versus operations. The larger the data set that you use, the more inefficient it will become. While there are more time complexity equations that exist such as o n factorial, which is even worse than the two I just mentioned, the data structures we'll be talking about, we'll never have time complexity equations that exists outside of the five we've just covered. So we're going to stop there. Now the final thing I want to say is just a reiteration of something I said before, these time complexity equations are not the only metric you should be using to gauge which data structure to use. As we get deeper and deeper into this series, you'll see that we might have some data structures which don't seem that efficient at all based on their time complexity equations for provide some other functionality or feature, which makes them extremely useful for programmers.
Okay, now that we have some knowledge on how we actually grade these data structures in terms of efficiency, let's hop into our very first data structure. The first data structure we're going to be covering is the array. Now I understand the array is a pretty common data structure taught in most programming classes, and that many of you might already know about the properties and uses of an array. But in this segment, we'll be going into more detail on the array as a data structure, including its time complexity equations, storage methods, and more. So check the description for a timestamp to skip ahead to that section. If you already know the basics.
If you're not as versed in the array, though, or just want a general review, then stick around, because right now we'll be discussing the basics of an array. An array is fundamentally a list of similar values grouped together in a central location, which makes sense because if you remember, we use data structures to store sets of similar information, so that we can easily use that information. Basically, arrays can be used to store anything, usernames for an app, high scores for video game prices for an online shop, pretty much any list of values which are fundamentally similar meaning of the same type. So integers, strings, floats, objects, the list goes on and on.
Any primitive or advanced data type you can think of can be stored within an array. Every item contained within the array is referred to as an element of that array. And we call the collective total of elements, the array, this is going to be true for almost all data structures we talked about, by the way, so just keep that in mind. An array will also have three attributes associated with it, the first being a name for the array, the second a type for the array, and the third a size for the array. Let's start with the name.
The name, if you couldn't tell is just simply a name for the array, which can be used to reference and interact with it. Say for example, we had an array called names, which contains the list of company employees. And then we also had an array called salaries which contained a list of salaries for those employees. If we wanted to search through the list of salaries, we would do so by referencing the salaries array with its name. This way the computer knows that you want to search through that specific array containing salaries, and not the names arraigns that
this works vice versa as well, of course, if we wanted to search through the names array instead. Now another thing to note really quickly, while we have this up is that these two arrays are also an example of parallel arrays. Parallel arrays are two or more arrays containing the same number of elements and have the property wherein each element in one array is related to the element in the other array, or arrays have the same position. So from our example, we can say that the first element in our names array, john smith, corresponds to the first element in the salary array, the integer 10,000. This would mean that john smith has a salary of 10,000.
This is really good, because as you'll see, later on, we can't store different types of variables in the same array. So we couldn't have the salary integers be stored in the same place as the name strings, making parallel arrays extremely useful for storing different types of data about the same entity. Alright, tangent over, let's get back to the attributes of arrays. And the second one, we'll be talking about the arrays type. Now an arrays type is simply what type of information is stored or will be stored within that array. One of the biggest stipulations about arrays is that it has to hold all the same type of information. So you cannot have an array containing integers and strings, it would have to be either an array of only integers or only strings. This works for any
type of information you would want to store within the array, meaning every element or spot within the array must be of the same type. The third and final attribute that an array has is a size. This size is a set integer that is fixed upon the creation of the array. It represents the total amount of elements that are able to be stored within the array. An array size cannot be changed. I'll repeat that again because it's extremely important and array size cannot be changed once created. Once you write the line of code which instantiates an array You give it a defined size or fill it with elements until it has a defined size. And then at that point, the array size cannot be changed by conventional methods. This is actually pretty rare for a data structure. And it may seem really counterintuitive and useless. But later on when we talk about an arrays efficiency, you'll see that this is for a specific reason, and not
just to be annoying to the programmer. So there you have it, the three attributes of an array, a name to reference it a type to fill it with an A size to control it. Let's now shift focus and talk about how we actually define an array ourselves how we reference and change values within an array, and then dive into the idea of two dimensional arrays. There are actually two different ways to instantiate an array in most languages, you can either populate the array with elements that you want contained within it right then and there. Or you can set a specific size for the array, and then slowly populate it later on as the program runs. We'll be discussing how to do both, starting with the first creating the array with the elements already stored. Now defining and filling an array as soon as
you create it is mainly used for when you already know which values are going to be held within it. For example, let's say you're creating an array, which is going to hold the salary of 10 different workers at a company. And that information is being read in from a text file. Well, you already know the 10 salaries for those workers from the text file, and so you're able to immediately populate the array when you create it. This way, you'll already have the information available to you as the program runs.
The way you do this varies amongst different programming languages. But it's shown here in three different languages, Java, Python, and C sharp. To give you a basic gist of how this is accomplished. All three lines of code on your screen now will create an integer array of size three, containing the numbers one, two, and three. You can see that all of them involve the declaration of an array name, which is then set equal to the values you want to include within the array encapsulated by some sort of brackets or braces. This isn't 100% how it's going to be for all languages, but for the most part, it will follow this skeletal outline. As you can see, Java and C sharp require you to define the array type, whereas Python does not. But for the most part, the syntax is the same.
Again, that is name of array set equal to list of comma separated values encapsulated by brackets or braces. This also develops the size of the array for you automatically. So since we populate the array with the numbers, one, two, and three, the array would instinctively have a size of three, as it now contains three integers. If we were to say go back and add in a fourth number, where we define the array, the size would dynamically increase to for the next time we run the code.
Now the second way in which we can instantiate an array is by setting an initial size for our array, but not filling it with any elements and then slowly populating it as the program runs. This would be used in the case that you don't actually know what information you're going to store in the array, but will as the program runs through. The most common case in which this happens is in the case where the end user will enter information into the program. And then that information will be stored inside the array.
Since the information varies based on which user runs the code and what they input during that particular run, we have no way of populating the array as soon as it's instantiated, forcing us to add the information later. shown on your screen now are two ways in which we can do this in Java and C sharp. Now you'll notice that there is no Python section because of the fact that creating populate later arrays is not conventionally possible in the base version of Python, without the use of more advanced data structures or packages. You can see However, with Java and C sharp that just as with the other example, there are slight differences in the syntax on how we do this, but it generally follows some set pattern, the type of the array followed by a name for the array, which is then set equal to a size for the array.
Remember, this size is final and cannot be changed outside of this initial call. If you end up creating an array with the size 10. But then later in the program, find that you need more space for it. You can always go back to where you instantiated it and change the size. But after that line of code runs, there's no conventional way to increase it. You may also be wondering what the brackets mean when instantiating an array and that's true Way to signify to the computer that we are indeed trying to create an array and not just a variable. Now that we know the two different ways to instantiate an array, we need to know how we, as the programmer actually get information that is stored within the array so that we're able to use it. And the simplest way to answer that question is through the means of a numerical index.
an index is simply an integer, which corresponds to an element within the array. Now, the most important thing to know about indexes is that for a certain array, they begin at zero instead of one. So if we have an array of 10 elements, the first index would actually be index zero, the second would be index one, and so on all the way up to the ninth index. It's a little bit weird at first, but trust me, you get used to it. Now, to retrieve information from a certain position within the array, you would reference it using both the arrays name, and then the index number of the element you wish to retrieve. Say we have our array called numbers here, which contains the integers one through 10. To print out the element at the fifth index, in this case, the number six, we would reference the array name, in this case numbers. And then in a set of brackets, we would place the index we want to retrieve,
in this case, the number five. What this piece of code is basically telling the computer is to print out the fifth index of the numbers array, which again is the integer six. Now because indexes start at zero instead of one, it's very important to note that to get the 10th element of the array, you would actually need to reference it using the number nine since there is no 10th index. If you do end up making this mistake, it will result in an array out of bounds error, since there is no 10th index in the bounds of the numbers array.
Referencing and arrays index number is how we actually replace elements within the array as well. Continuing with our numbers array, let's say we wanted to change the last element, the integer 10 to be the integer 11. Instead, what we would do is reference the ninth index of the numbers array, the one which currently contains the integer 10, and set it equal to 11. This tells the computer to take the element at index nine and replace it with the integer 11. Essentially swapping 10 for 11. Now the last thing I want to talk about before we jump into the time complexity equations for an array is the practice of putting arrays inside of arrays. An array with an array at each element is known as a two dimensional array.
Think of a two dimensional array as a matrix. It's similar to any other array, except for the fact that instead of a primitive data type like an integer how's that each element, we would instead have a whole other array with its own size and indexes. two dimensional arrays are useful for a variety of purposes, such as if you were programming, a chess board, a bingo board, or even an image where each element in the two dimensional array contains an RGB value, which when combined creates an image. Now referencing an element within a two dimensional array is mostly the same as with one dimensional arrays, except you now need two indexes, the first number would be the index of the column that you want to reference, and the second number would be the index of the row that you want to reference.
By combining these two numbers, we can single out an element within the two dimensional array that will then be returned to us. As an example, let's create a two dimensional array with 16 names contained within four rows and four columns. Now to access the name Carl, you'd first single out the column, which contains the name you're looking for. In this case, Carl is in the third column, meaning it's in the second index. Next, you would single out the row which contains the name you're looking for. In this case, the name Carl is in the third row. So we would again use the second index to reference it.
Combining these two pieces of information gives us the index location two comma two. And if you look, Carl is indeed at index location two comma two. Just as another example, Adam is in the first column two rows down. And so to reference Adam, you would simply call upon the element at index locations zero comma one. two dimensional arrays are just the beginning. You can also have three dimensional arrays, four dimensional arrays, and so on for containing large amounts of advanced relational data. But two dimension is where we're going to stop for today.
All right, that concludes the background information on arrays. Now that we know what they are, let's actually talk about arrays as a data structure. This is where we're finally going to be using our big O notation knowledge from the previous segment. to judge the array as a data structure. We'll be going through the four criteria we talked about previously, accessing elements within an array, searching for an element within an array, inserting an element into an array, and deleting an element from an array, and scoring it based on how effectively they can complete these four tasks using time complexity equations. Now accessing an element within an array can be conducted in an instantaneous of one time. This means that for any element within your array that you want to know the contents of, you can do so immediately by simply calling upon its index location.
This has to do with the way that an array is stored within memory. For example, sake, let's look at a fake portion of memory in the computer. You can see we have some information near the beginning, maybe some integers or strings that we've initialized, that's not important. What's important is that when we get to the part in our code, which instantiates an array, we already know exactly how large the array is going to be. Either because we've populated it with the elements needed, or given it a definitive final size. This means we also know exactly how much space we need to reserve in memory for that array.
Combining these two pieces of knowledge means we can instantaneously figure out the location of every single element within the array by simply taking the starting location of the array in memory, and then adding to it the index of the element that we're trying to find. So let's say our array contains three elements, each of them integers. And we want to know what the element at the first index is. Well, we know that the array start storing data here. And we also know that every element within the array is stored contiguously together in memory. And so by taking the starting location for the array in memory, and adding one to it, we now know that that's where the first index of our array is going to be, and can return
that stored value appropriately. This is the main reason why array sizes cannot be changed. All of the information within an array must be stored in this one place. So that we're able to do this, the contiguous structure of an array prevents you from adding space to it after it's been initialized, because it literally can't without breaking up the data and adding in the space to store new elements down the road away from the initial array. This would make accessing elements instantaneously not possible. Hopefully now you can see why both accessing an element within an array is O of one, as well as why array sizes are final, because not many other data structures allow you to have instantaneous accessing power, meaning arrays have a huge advantage on the others in this metric.
Next up searching through an array is O of n. This is because for the most part, you will be working with unsorted arrays, those being arrays which are not in alphabetical numerical or some sort of quantifiable order, meaning that to find an element within the array, you may have to search through each element before you can find it. This is known as a linear search. And if you want to know more about it, click the card in the top right corner, and it will take you to that section in our introduction to programming series. A link will also be provided in the description below. Now while there are faster ways to search through an array, those only work when the array is sorted. And so for a basic array, in worst case scenario, searching through it to find a particular element is going to be O of n. Now Sure, there are going to be cases where the
element you're searching for is the first element in the array, or even somewhere in the middle. But remember that when working with big O notation, we always use the worst case scenario. And in this case, the worst case scenario is that the item that you're searching for ends up being the last element within the array, which for an array of size n would take n operations to reach. Finally, inserting and deleting elements from an array both have a time complexity equation of O of n for inserting this is because adding an element into the array requires you to shift every element that's after the index, you want to insert the value at to the right one space.
For example, if you have an array of size five, currently filled with four numbers, and you want to insert the number one into the array at index zero, you would first need to shift every element to the right of the zero with index right One, essentially freeing up the space for the new integer, while also retaining all of the information currently stored within the array. Then, and only then can you actually insert it. This requires you to traverse through the whole array of size n, which is why the time complexity equation is O of n. Now, sometimes you might not have to shift the whole list, say in the case, you had the same array of numbers, only this time the fourth index was free, and you wanted to insert the number five in that open spot.

Ask anything
We've read this whole study set, so questions about it get the best answers.