Arrays
In this given portion of the course, he plans to talk about searching, that is that given a certain amount of data, what is the best way to search through that data for a given piece of information or an outcome.
You can search through different abstract data structures in different ways, depending on the structure, and he is also going to talk about how to actually implement all of this shit in code.
An Abstract Data Type is basically something that is not outright an implementation of information in a coding surface, and is instead just a description that is used to abstract out the implementation. It would feature things like aspects and possible operations that you can perform, some examples include: Boolean, Integer, Vector (arrays fuck u), Queue, and Stack
A Data Structure is the actual implementation inside of the code. Basically the way in which you can implement the abstract data type. So the way that a stack has that given pointer in the array and the way that you can use it basically. An example is basically an Array as well.
An Array is basically a buffer of memory that is set aside inside of your computer’s RAM. It exists as a contious chunk of memory that is generally allocated to store a single size of a given data type. So something like a char array would be a buffer of memory that you can skip around in one byte at a time, while an int array would be skipped around by 4 bytes for example.
Some programming languages allow you to have different types of memory inside of a given array, but in all honesty thats kind of that magic union bullshit where it isn’t really an array anymore and you just waste a ton of memory, maybe at the very least. But in essence an array is a buffer of memory that you have a pointer into, and by knowing the sizes of the inner elements, you can skip between different ones, allowing you to have this kind of a list of elements
In ALMOST all programming languages, you index an array from a value of 0 onward, meaning that the highest you can index is length - 1 of the array before going out of bounds. One an array is created, you can do things like read values at a specific index, and then also write to those values at a given index.
Sometimes dynamic arrays exist, which is basically when the underlying memory management of the array is pretty smart, and so it keeps count of the number of elements that it has as well as the pointer to the buffer in memory of where the data is stored. Each time you want to write into the array, what happens is that it would check out how much space it has, and if it needs more, it would grow the array by allocating another buffer, copying the current contents over to the larger one, cleaning it up, and then adding in your element.
Dynamic Arrays:
We can use something concrete, like the array, in order to make more something abstract, like the dynamic array. Given an array or a vector, you actually cannot change the number of elements that exist inside of them, unlike how you can in a stack or a queue.
The dynamic array is an array that can dynamically change size. It has the same operations like a regular array would have. It has the ability to be empty as well, but it has the operations: length, select[k], store![n, m]
The length operation can actually change as the dynamic array exists, because it has the ability to change size during runtime. Because of this we need a way to change this size which is done with the two new different operations that we have: insert[l, k] and remove[k]
As it suggests, this either adds in a new element at a given index, or it removes an element at a given index. The issue here of course is that if you are adding something or removing something, all of the iterators you had before hand change indexes. It can also make it pretty weird with how they have it, for example python has append which just adds in the value at the last index, sort of like a stack. This way you cannot have index 10000 with like 9997 unused spaces wasting your memory. Obviously as the coder you need to be aware of this, but from a language decision standpoint its also obvious to not just let the user store an element whenever the fuck they want.
As I have said, the best way to actually implement this under the hood is with a regular array. Every single time that you want to add in a new value to your array, what you do is you create a separate array of that size or more and then you allow the user to append to that array. If you want to remove it, then you can either resize the array and then move all items, or move all items back by however much was removed
Linear Search Algorithm:
You basically loop over the array until you find the element that you want, and then if you find it, you return it and then if not, then you know it isn’t in there. Generally this is O(n) where n is the number of elements in the array that you are searching.
The issue is that this is pretty fast kind of, and is the fastest searching algorithm for whenever you’re array of data is unfortunately unsorted. This is good generally for only if you have something like only a single time you need to search through this data.
If you need to search through the data multiple times for whatever reason, it might be best to sort the data first in O(nlogn) time and then use the binary search to search for your elements in the very very fast O(logn) time.
The same thing applies to all of the other linear data structures like stacks or queues You can only search through them in a linear manner at the fastest, and that is because of the fact that they are just arrays underneath. Of course the different can come with how the data is structured where it might match a stack better or something, but the concept of searching still remains only as fast as O(n):
Searching Stacks and Queues:
So basically what you can do is store the data in another stack or in another queue, basically another array because fuck you buddy thats basically what those are, and then you can still just search them normally.
You look at the top value of the stack, if it is the element you are looking for then you are all good, if not then you copy it to the temp stack or the temp array, and then pop it off the stack and look at the new top. The same with queues with the tails and heads shit. Then once found, you can iterate over your temp stack and have it be your regular stack.
This is all of the O(n) time still, nothing changes, it still takes the time of the given n elements
Book Reading:
Please read Section 10.1 of:
Cormen, T.H., C.E. Leierson, R.L. Rivest and C. Stein Introduction to algorithms. (Cambridge, MA: MIT Press, 2009) 3rd edition.
In this reading task you should get a good idea of how stacks and queues are implemented within simple arrays. The concept of a pointer is also used, which is just a reference to the address of some memory. We will come back to pointers when discussing linked lists. For this reading you can see the pointer as just storing the index of the array.
Note that in Cormen et al., the first memory location in an array is indexed by 1, and not by 0 as in this course. If you wanted to recover the arrays in this course, then you just decrease the index by 1. The reason for this convention difference is when we think about implementing vectors with arrays. Remember that vectors have the first index being equal to 1 in this course. In Cormen et al., the length of the array is assumed to be stored in memory somewhere, but when we implement a vector with an array we make it explicit that the length of the vector is stored in the element of the array with index 0.
Book Notes:
Stacks and queues are elementary data structures that server as a representation of data using a set and a pointer. In a stack the element deleted from the set is the one most recently inserted, implementing the last-in, first-out or LIFO policy. A queue however is different in that the element that is deleted is always the one that has been in the set for the longest amount of time, this follows a first in, first out / FIFO policy.
Stacks
The insert operation on a stack is often called push and the delete operation is often called pop. These are allusion to real physical stacks basically, such as a stack of plates used in cafeterias or something, the plates are popped from the stack in reverse order to how they were pushed onto the stack, as only the top element is ever accessible.
The following is an implementation of a stack. You have a continuous buffer of data, basically this array, and then a pointer that is pointing to the last element in the array. The way that this works then, is the element that the pointer is sitting at is the “top” of the stack.
If you then want to push a new element onto the stack, you move that pointer over by one space, and then write the new piece of data into that spot, doing so then makes this new item the new “top” of the stack, and this is push operation.
Then, if you want to pop something out of the stack, what you can do is either return the value of the current top item (depending on the implementation, C++ just moves back the pointer), and then move back the pointer to the previous element. The reason this works without having to clear the previous top value is because it gets overwritten every-time that you add in a new element.
Obviously this comes with caveats, for example what if you want to pop an element when there are no elements in the stack? Well in that case we then say that the stack underflows which is generally considered an error. Equally what if you want to push more onto the stack then you have allocated space for? if your stack does not know how to resize itself then we call this overflow or the very famous website named after this stack overflow
Below is an implementation in pseudocode of the stack:
Queues
So queues are a little bit weirder, at least in my opinion, they share the same idea of a push and pop operation except that they are instead called Enqueue and Dequeue respectively, each working as you expect them to.
The way that the actual underlying operator works is you have this head and tail index pretty much right, these are two pointers, one is pointing at what is basically the first element that was put into the queue, and then the tail is pointing at the spot after the latest element going from the queue. It is worthy to note that this order works, at least in the image above, to have the head reading from left to right instead of kind of how a stack foes right to left.
Well basically what happens here though of course, is you have the tail pointer which is pointing at the next empty element in the right of the array, and whenever you enqueue an element into the queue, this element is placed at this tail pointer, and then the tail is indexed by one, if the tail index plus one was to overflow the array, it wraps around back to the front, kind of like a hashmap.
That is how the tail works, and then the head is what uses the dequeue operator, it points to the element that has been in the queue the longest, and so the one that the tail places the most operations ago. It always points at the actual element so it can read from it, but when you dequeue the element, it gets read and then the head pointer moves on to the new next head.
This allows for the queue to sort of work it’s way around the entire buffer of memory, meaning that it can have those weird looking gap intervals like you sort of see in the graph above. The initial state of the queue is where the head and the tail are pointing to the same direction, and this means that our queue is empty.
This way if we try to dequeue the empty queue, then we get an underflow error because the head would not have anything to read and we are out of sync. Similarly, if we try to add in an extra element when the head equals the tail plus one or the head equals 1 and the tail is the length of the array, you get a queue overflow error.
Here is the pseudocode for a queue:








