Now that we have a good understanding of.
Sorting algorithm like selection sort.
We can continue on and learn additional sorting algorithms.
In this lecture, we are going to begin looking at what bubble sort is.
And bubble sort is one of the simplest sorting algorithms because it just repeatedly swaps the adjacent
values if they are in the wrong order.
So it's not going to be super performant for large data sets, but it's still a good sorting sorting
algorithm that we can evaluate and learn.
So we'll create ourselves an array here and we'll give it five, four, one, three and two.
And remember, what we're going to do is sort adjacent values.
If they are in the wrong order.
So we see here.
So we'll start at five and zero and we see that our adjacent value is four.
So now we can swap five and four and then leave the rest of them the same.
And we're still on value five because we have not iterated through the entire array.
So now we're still at five and we're going to swap now with value one because one is less than five.
So now our array is 415, three, two.
And again, since we haven't completely iterated through the array yet, we are still on value five.
So now we will compare five and three.
Three is less than five.
So now we will swap three and five.
So now we have four one, three, five, two.
And now that we're almost at the end of the array, we have one more check that we need to do.
And we need to check if five is less than two, which it is.
So now we have four one, three, two and five.
So now we have completely iterated five through the array.
So now we know that index five is in the correct spot.
Five is in correct spot.
So now we're going to go back and we're going to start over with four.
So now we're here at four and we're going to compare it to one.
So we know that one is less than four.
So now we go one for three.
Two and five.
We're still at four.
So now we compare four and three.
So we have one, three, 4 to 5.
So you're starting to see why this algorithm might not be the most efficient one of all the sorting
algorithms that we will learn.
So we're still at four.
So four is less than two.
So one, three, two, four, five.
We see that.
We know that five is in the correct order already, so now we're done with four.
So now you're quickly starting to see the pattern here.
So our next value is one, we compare 1 to 3, two, four and five.
So now we know that one is in the correct spot.
So now we compare three and two.
So we know that we need to go one, two, three, four and five.
And now we have our sorted array.
So we are now done with sorting this array.
So again, now you can see why it is not the most efficient algorithm, but let's look at some pseudocode
to see what it would look like, not a B.
So we want some pseudocode.
That way we can have a blueprint for what we want to code when we go to do our implementation.
So we know that we started at iteration one, so this would be one for loop.
So we'll say for AI to excuse me for AI in zero.
To size minus one.
And then we're comparing it with all the elements adjacent to it.
So we're going to need another four loop.
So we'll say four j in.
One, two.
Size minus two.
And then we will do our compare.
J.
I compare J If it is greater than J plus one, and if it is, then we want to swap J and J plus one,
and that is basically our pseudocode for this.
So let's do one more example.
That way we make sure that we have a good understanding of how this algorithm works now that we kind
of know what the pseudocode is.
So we will do.
An array of values of five, one, four, two and eight.
So this is our unsorted vector of values.
So we will start here.
And then this will be our AI plus one.
So now we we compare we see that we need to do a swap.
Four, two and eight.
So we're still here at five Compare.
We need to do a swap now.
We're one.
Four, five, two and eight.
So we're here still.
Compare one four, two, five, eight.
And now we know that five is in the correct spot because our next comparison will not yield a swap.
So now we also know one is in the correct spot.
So now we need to start here and we will evaluate two and four and we see one, two, four, five and
eight.
So that was a very simple one and we were able to quickly see how this works.
It works really well with arrays that are already kind of sorted because as you can see, we had way
less iterations needed here than we did in our array up here.
So now that we have a really good understanding of how bubble sort works, let's go ahead and begin
implementing this algorithm in the next lecture.