 Okay, in this lesson I want to do a quick overview of insertion sort, I'm going to talk about how it works, and I am going to show you how to code it in Java. So let me just refresh this page real quick, and we'll take a look down here. Now this is from the Wikipedia, so it's free and open, of course it's not letting me restart it, so let me do something a little there, and reset that list, and there is insertion sort. So basically what we're doing. We're starting here, and then of course that's the first spot, then we look here. Is this less than that number? If so, we swap it. Now we come out here, and we look, is it less than that? Yes, is it less than, or actually is this greater than, same thing, and then you swap. Basically you swap until you either get down to the bottom, which case you're at the zero index, or it's no longer greater than. So in this case, you'll see eight is already greater than six. We don't have to swap it. Seven, eight is greater than seven. So we swap that out, and now here we see it. So we're going to take two. So two is eight greater than two, yep, swap. So now two is here. Is seven greater, yep. Six greater, is five greater, is three greater, yep. One greater, nope, done. Okay, so you're still swapping it on each occasion. And then you do the same thing with that process. So you can see that you're keeping track of a couple different things. You're keeping track of where you are along here, because we're going to the right, and then we're starting from whatever that point is, and we're going to the left. So we're gonna need two variables. So we'll use i to go to the right, and we'll use j to go to the left, to keep it consistent with this pseudo code. So there's some pseudo code here, and again, this is from Wikipedia, so this is non-copyrighted, which is nice. And so the code is, we start at one. Okay, so i is gonna be one. And as long as a is less than the length, we're gonna go all the way to the end. Again, a is going up, or sorry, i is going up. Then j equals i. So j and i are gonna both start here at one. Is j greater than zero, yes. Is j minus one greater? If so, we swap it. And then we take j, and we move to the left a little bit, and we do the same thing. We check and see, are we still greater than zero? Is it swappable, or is the ones on the left greater than the ones on the right? If so, we swap, we do the whole thing. Then we move i over one and repeat the process until we reach the end of the, array, okay? So let's go ahead and take a look at this. I'm gonna be using j-grasp. This is what I've been using all year. And I've already set up an array for you. And so we started out with int i equals one. Notice we're not starting at zero, which is kinda interesting. Because we're gonna be comparing one to zero here in a moment. And then so while i is less than a-r-r dot length, oops, I've been doing a lot of Python lately. So you'll probably see me put a lot of colons in there. And so array dot length, probably we don't need that. And sorry, I said I've been doing a lot of Python, so I've been forgetting my Java, my Java style. So while i is less than array dot length, I hope it's length, find out here in a minute. Let's go ahead and run it and just compile and see what happens. Okay, that's good, that is good news. So while i is less than a-r-r length, we're gonna set j equal to i. Okay, I'm gonna go ahead and up here and say int j equals one. So we know they're both starting at one. And then, again, the key thing is, while j is greater than zero. So we need to make sure j is greater than zero because we're not going all the way to zero, we're just going down to one. So that's gonna be our last swap. And then we need to check and find out, okay, is the array j minus one is the space to the left, the cell to the left, how are we gonna call it, greater than the current one. If so, we do a swap. Okay, so I'm gonna go ahead and make an int, and I'm gonna call it a temp. And so what I'm gonna do, so we have a swap when you, a temporary variable. So I'm gonna say that a-r-r-j, actually, sorry, temp equals a-r-r-j. a-r-r-j equals a-r-r-j minus one. Okay, and don't forget your semicolon, like I'm doing. And then we say a-r-r-j minus one equals temp, okay? So we've basically swapped out the values. Now, we need to decrement j, but now we've made a swap, j's gonna go to the left, and now we're gonna do the same check, check the one to the left of that and see if we need to do a swap. Now once all the swapping is done, we increment i. Remember, i is starting at the left and moving all the way to the right. j is starting wherever i is and moving to the left. So that's why j is decremented and i is incremented. And that should do it. So let's go ahead, compile it, see if we've got any compilation errors, good sign, and run it, and hopefully we will see an organized ordered list. Fantastic, there you go. Okay, so yeah, if you have any questions, let me know. Otherwise, you can go back, watch the video, check the Wikipedia page, but otherwise it's pretty straightforward, okay? There's a couple of key things. Remember, we're starting at one, not at zero. If you start at zero, you'll end up with an out of bounds error. Remember that i and j start at the same place, but i is moving to the right, so it's increasing. j is gonna be decreasing until it gets down to one and i is gonna increase until it gets to the array length. And then while j is greater than zero and this is the key, the one to the left is greater than the one to the right, we gotta swap them. And then just repeat the process until everything is done, okay? Thanks for tuning in. Good luck.