OK.
Welcome back to another lecture in today's lecture is going to be pretty short, it's actually just
a sorting challenge that I have for you, and I know we haven't talked about sorting, but we are going
to talk about sorting.
And what I want you to do first is challenge yourself and see if you can actually come up with a sorting
algorithm on your own.
And so that is what I'm going to challenge you to do today.
So let's get into it.
So what I want you to do is try to make a program that takes in five integers and five.
It can really be any integers if you want, but just to make it simple so you can like trace it on paper
if you need to.
Why don't we go ahead and start out with just five integers?
So taking these five integers from the user, read them into either an array or vector.
It's your choice, and then your program should sort the numbers in that container.
So the array or vector before then printing them out.
Here is a sample running of the program with the input and output, so you notice that it just repeatedly
five times asked for a number.
So the numbers are being read into an array of vector in this order for one nine three two.
Yet the numbers then get printed out one two three four nine, which is in sorted order.
So there's a few tips I can give you for this.
I won't give you a ton and this might be really, really hard.
So if you're not able to figure something out, I don't expect you to be able to necessarily do it in
a short amount of time or even do it at all.
It's just to kind of try and challenge yourself to think of how what would I do to sort these things?
You do have all the tools necessary to sort a vector or an array.
It's just kind of a little tricky to think about how it works, but I'm going to give you some tips.
So a big thing is swapping positions of the array or vector.
It's definitely going to be necessary.
So how do you do that?
So you can do something like, you know, let's say you have some vector like my event, I.
And then you say, you know, my vector, I got my vector plus one.
So all this is saying is that whatever position I'm looking at right now.
I want to take whatever is right past it.
And I want to.
Copy that into, you know, the one I'm currently on, and then this right here is kind of doing the
opposite, so you could, you know, do the vice versa, but you should really think about whether this
would work if you just had these two lines in a row.
Would that really swamp them?
So what you should probably do if this seems confusing to you is draw your a vector on a piece of paper
with an example and then one line at a time.
So let's try and do this line first and then do this line afterwards and see if you are able to successfully
swat them.
You most likely are only able to update this line, but not vice versa.
So that is a tip that I have for you.
Another tip is to use loops.
Of course, you're going to want to go through your array or vector, and you're going to have to move
things around, right?
That's what the swapping is.
And you know, that movement, while swapping is what can really get you a sorted array or assorted
vector.
So think about this a lot.
I'm not going to give any other tips or hints, but I think with what I've told you so far, you can
figure it out.
You do not have to solve this like in just a few minutes.
It can take you as long as you need.
It might take some of you only, you know, five to 10 minutes.
It might take other people several hours.
It might take other people a full day of work or two days of work, you know, depending how much time
you have, if it's very confusing.
But just take all the time you need, but really think about it, drawing things on paper is your friend,
you know, draw out the little boxes for the positions of the array and then have a little table, maybe
where you record what your variables are.
For example, if I is changing.
Say what is each time a loop iterate?
So each loop.
Take, I am big, what is I?
Oh, what is my vector I?
What is my vector?
I plus one or my vector I minus one?
I'm not saying that you have to use I plus one.
You can also use a minus one.
Just make sure you're recording and keeping track of those variables.
So you're noticing how it's changing.
And when you write your code, you can trace it on paper and draw out the array and show how the array
changes.
Each time you want to redraw your array, every single loop iteration to see how it's being sorted step
by step or if something's wrong, that's a good way to debug it.
Also, remember that see out statements are your friend as well.
Printing out stuff in the loop that you're confused about, like you're not sure how my vector is changing,
then print it out every time.
Throw out it, throw in a little see out statement there so you can see how it's changing.
All right.
So with that, I will let you go.
And in the next video and the next subsequent videos, I'm going to show you a few different sorting
algorithms and just in general, how we can think about sorting things in an efficient manner.
So do not look up any answers.
OK, do not look up sorting algorithms.
Try and come up with your own sorting algorithm.
All right.
And with that, I'll see you in the next lecture.