Bubble Sort
So you want to sort data because it makes the data easier to search when you have a predictable method of knowing things about the value that is about to come up next. There are different types of sorting based on what you might want to do, for example for numerical values you might want to sort them least to greatest, but for strings or characters you might want to sort them alphabetically For now, he will only talk about integer/numerical values.
Bubble sort within of itself works by looking at the current element as well as the element in front of it, if the element in front of it is smaller than the current element, then you proceed to swap the two elements, and then you increment by one, and then you basically always move the larger one forward.
So you take the largest element and then you move it to the right place it is supposed to go, and then you do so for the next largest and so on and so forth. This is an O(n^2) operation because you compare throughout the array for every element in the array. So if the array is in reverse order, this is the worse case, this takes the entire O(n^2), technically it can always take that amount if you code it up without much though and always do the less comparison, but it is still less overhead since you do not need to swap elements
This is the pseudocode for the bubble sort algorithm right, you first end up getting the length of the array because of course you do, and then you have 2 nested for loops, the inner one does the comparison, and each end of the outer one is one element being correctly sorted right.
Basically though, inside of the second loop, you just see, “hey is the current element larger than the next one”, well in this case it is, “hey is the next element smaller than this one”, and if so then you swap them with the whole temp thing and what not right
They also have this count variable here pretty much, which is basically just that thing of making sure that we are not always at an n squared solution, because you know that if you made no actual swaps with the current element that you are on, then the array must be solved in that case, so you can just return the vector as is
Notice also the n-1 for the length, this is so you do not index outside of the array of course
This is just a flowchart for how the bubble sort algorithm ends up working.
Bubble sort on a stack
So basically you get your stack right, and then what you do is you take the first element, the first top of the stack, and then you pass that onto another empty stack. You then ask if the top of the second stack is smaller than the top of the first stack, if it is, then you do not need to swap the values on the top of the stacks. So you then push the next element from the first stack onto the second.
If the item in the second stack is in fact larger than the top of the first stack, so you swap the values on the top of the stack, and you basically keep on doing this thing with swapping the tops of the stacks until you end up with the larger and larger elements towards the bottom of the original stack, I mean I vaguely get it, but this does sound a bit weird lowkey, here is the pseudocode: except he doesn’t show it, so fuck you bud
Insertion sort
This one is a bit of a reverse insertion sort sort of. Basically the way that I believe it works is you look at element in front of your current element, and then you see if it is smaller than that current element, if it is then you do that, and you continue to do swap it over and over until eventually the element is in the right place.
This is really good for small data that is almost sorted, and is why it is used in some optimized arrays like tim sort or the better versions of merge sort and all of that bullshit, I think both of those are tim sort actually. Either way, in the worse case, with the array being sorted in reverse order, this ends up being pretty slow at O(n^2) once again, but it is generally on average faster than bubble sort just because you tend to do less operations with already sorted data.
It’s basically just a for loop with a while loop inside of it where you just keep on swapping the element back until it is between an element that is smaller than it and an element that is larger than it. The best way people have generally stated this to the regular world is by having a hand of cards, and you sort it by just basically doing insertion sort man
Obviously you have the few edge cases like making sure the smallest element doesn’t cause the greatest loop and all of that stuff, I don’t know what the fuck that shift function does, but I basically just assume that he swaps the elements
The missing bit is: vector[i-k] <- vector[i-k-1]



