 Let's start our discussion of sorting by introducing an algorithm or a method to shuffle an array into some random order. What we're going to do is we're going to take this array of countries, which happens to be in alphabetical order. We'll shuffle it and then print it out. And then we're going to sort it back into order using the selection sort and print it out again. So that's the plan. And here's the pseudo code for the shuffle algorithm. What we're going to do is we're going to have for each index I so that means for it is zero. Oh, we're guess we're going to have to have the name of an array here, aren't we. So we're going to have a string array called elements or call it array. Let's call it ARR. That'll work. Work just as well. Where I is less than array dot length. And we go to the next one. Now we need to choose a random number, which means, which means we need a random number generator. And we've imported Java dot util dot random. So let's create a random generator is going to be a new random number generator. And then what we're going to do is int position is going to be generator dot next int array dot length. And the next entity gives from zero up to but not including the number you give, which is exactly what the pseudo code says we're supposed to do. And then we're going to swap position I and the randomly chosen element. So that means we're going to say string temporary becomes array sub I. Now that we've saved it we can wipe it out and put in the array at our random position. The array at the random position becomes the temporary string that we had. This is going to shuffle the array in place. It's not going to create a new array. That's why it's a void method. Let's compile that and see what happens when we do that. Well, that's exciting. Oh, okay, let's see what's going on your non static method. Yes, I forgot to call it static. And then I also forgot to put in our selection sort. I'm going to put in a placeholder. It's also going to be void. And we'll call it selection sort. And it's going to take an array of strings. And for now, we'll just say, leave the array unchanged for now. And in fact, let's comment this out so that we don't have to see things twice. Yeah. Now it's compile. That's much better and we execute it. Sure enough that we have it in a totally different order that is absolutely not alphabetical. If we were to run this again, we get a completely different set of country order. So good, we can shuffle things. Hooray. Now the question is how does the selection sort work. And this has gone over in the book and I also have a video. There's a million videos for selection sort. So I'm going to go through this rather quickly. Here's the idea. I'm going to start at index number zero. And then I'm going to find the smallest item in the array. The smallest item in the array is ant. I'm going to switch ant with position zero. Now ant is in the correct position and I move on to cat. Starting there, I go and find the smallest element in the array, which is B. And then swap those. B is now in the correct position. I move forward to dog and starting from dog. I want to find the smallest element from dog on towards the end. And that is cat. I swap cat and dog. Then I proceed to Fox. And from there I find the smallest item, the smallest item is dog. I switch dog and Fox. And then that's starting at Fox going to the end of the array. Find the smallest item, which happens to be elk. And switch them. And since I'm at the last item. It must be in the correct place. So here I have everything in the correct order. So that's the idea. And in fact, that is exactly what I have done here. I've put this here so that I can remind myself how this works. Here's what we're going to do. We're going to say for its starting position is zero. Start less than array dot length minus one. Why? Because when we get to the array length minus one, the last one is going to be in the correct place. We don't have to move it. And we're going to have start plus plus. Now we're going to need two variables. We're going to need a string called the minimum value. And we're also going to need an integer, which is the minimum index. Where did we find this minimum value? Here's what we're going to do. We're going to take the starting point as our minimum value that guarantees that we are at least going to have something that's in the array. And also the place where we're starting might just happen to be the smallest item. So our string min value is going to be. Array sub start. And the index where we have our current minimum value is also start. Now I need another loop. I'm going to say. Let's call it the find a position. And we're going to start at start plus one. Find less than array dot length. Because we have to go all the way to the end this time. When we're trying to find the smallest item, we have to include the last one. And then find a plus plus. Okay. If the array. Item that we're looking at. Is less than. The. Array than our initial. Yes. Well, no, I'm sorry. This is not what we want to compare it to. We want to compare it to the minimum value. So. I'm going to let's go back and look at the diagram here. Essentially, let's say I'm at dog. I want to look at Fox and say is Fox less than dog. No. Is cat less than dog? Yes, it is. That becomes our new minimum. Is elk less than cat? No, it isn't. And therefore cat is our minimum. So if I find something is less than my minimum value so far. Then that's going to become my new minimum value. And the index where I found it is going to be at. That position when I'm done with this loop. I've guaranteed to have the minimum value in min value and the minimum index in. Min index. Now I can switch those around. And we're going to have a string called temporary, which is going to hold our minimum value. And then we're going to say. Let's do it with indexes. That means my array at the minimum index can now get wiped out. Because I've saved it away. And that's going to be the array at our starting position. And the array at our starting position is going to become the temporary. And that should do it. Let's compile this and see if I have any typing errors here. And now comes the big test to see if it works properly. Well, the reason it's not working properly is because I didn't uncomment this. It would be nice to. Nice to uncomment this so we can actually execute the method. Wouldn't it be? And sure enough, everything. Which we shuffled into a random order has now been sorted into the correct order. Let me just check something here real quick. Did I create a file for this or not? No, I didn't. Okay. The selection sort is. So I'm going to take an algorithm with a big O notation of order of N squared. That is the time it takes to sort. Is proportional. To the square of the number of items. So a list 10 times as long. We'll take up on. On the order of. 100 times as long to sort. Why is that? Because we have two loops here. This loop goes through essentially N times. And this one also goes through. Approximately N times and N times N is N squared. So it's going to be N squared iterations through the loop. Now, if you have a. Small. Number of items such as this one here. The selection sort isn't that bad. If we had an array of, let's say 20,000 items, then selection sort would not be what we want. We would want to go to something a little more efficient, such as the merge sort. And I'm not going to cover merge sort. It's in the book. If you skip reading it, that's perfectly fine. It's not going to appear on the test anyway. But it's worth knowing about. So skim it. And if you get totally lost, don't worry, it's okay. So there is our selection sort. There are all sorts of other sorting algorithms that you can use. With more efficiency than selection sort. But the nice thing about selection sort is it's fairly. Direct to understand. Let's see. Let's have a go through its paces. So we can see what's going on. Let's save this as selection sort example two. And this time, instead of a whole bunch of countries, let's just call this animals. And I'm going to do that example that I had in the slides. So we're going to have ant, the cat, dog, and we'll shuffle our animals and sort them. Let's put in some debug output so that we can see what's going on. What about that? And then we'll have current minimum is then in our inner loop, what I'm going to do is every time I find something that has to swap. Every time I find a new minimum, I'm going to tell it, tell us that. So system to add on print line of is plus. I'm putting some extra blanks in there so we can see that indented. And then once we're out of that, what we're going to do is we're going to say system dot print line of swap plus index plus start. We'll print out the arrays so we can see what the result is. And we're going to use arrays dot string. And then we definitely need an extra blank line in there for readability. So let's compile all of that. And now let's run it so we can see what's actually going on. So here we have and the dog fox cat. I guess I'd better. That's interesting. Huh. This is really weird. Oh, no, it shuffled them, but it didn't shuffle them very well. Okay, they're almost still all in order. Let's try again here, shall we? Okay, that's a lot better. So starting position moves to zero, the current minimum is be the new minimum after we go through our loop as aunt at location one. And so we're going to swap be an aunt. And we did not get the result there. Why? Oh, because I have done something very silly. That's why I printed this out. Before I did the swap, I have to print it out. After I do the swap. There that'll bell work a lot better. So sometimes you have to debug your code that you put in for debugging. Okay. Our current minimum is dog. The new minimum, it found be, but then it found aunt at location five. So it swaps dog and aunt. And sure enough, aunt and dog are now swapped. Now we're starting position moves to one. Where we have Fox. And the new minimum will be be at location two. And that's the minimum of all of them. So we'll swap be and Fox. And that's what we end up with. Starting position moves to Fox. The new minimum is cat at index three. And we'll swap Fox and cat, which gives cat and Fox in the right order. Now we're going to go to position four. Three, excuse me, the current minimum is now Fox. And I look as less than Fox. So that's our new minimum. And that dog is less than elk. So that becomes our new minimum. And we're going to switch dog and Fox. And here the starting position moves to four. The current minimum is elk. And we're swapping it with itself, which is a little bit ugly. So we're wasting a little bit of effort. Because elk is the minimum. And we really don't have to move it at all. Shall we put in some code to optimize that or not? Sure, why not? So here what I want to say is. If the minimum index. Is not equal to our starting one. In other words, if we found something different than our starting one, then we have to swap them. Otherwise there's no need to swap. Because we're switching it with itself. And that is a very tiny optimization, whether it's worth it to do that and whether it increases readability, I'm not sure, but I should probably put in a comment here on this one. Yeah. So swap minimum with current. Start position. But only if the minimum. Isn't already the starting position. And that is the selection sort. And I think I'm going to call it a day for chapter 13 here. I don't think there's anything else that I need to really talk about here. Um, you can read about the decks and merging and. Well, it's like for a quick static context. I think I talked about those earlier. Namely static methods belong to the class as a whole. You don't need an instance to do them. I'm not going to do a mini lecture tomorrow. I'll continue on next week with chapter 14. Well, okay. I take that back. Chapter 14 has a lot of extremely important stuff in it. Namely inheritance. And also in the book. There's a lot of stuff to cover. So once again, I think I'm going to get a head start on chapter 14 and start covering some of that. Tomorrow in a mini lecture. But that's it for tonight. See y'all online.