‫So now we're going to write our code for our pivot helper function.
‫We're going to run it on this array, which I will shrink down into the corner.
‫And the first thing I want to talk about with Pivot is that there are two different places where we
‫swap items in the array, and we can do that with something like this.
‫But because we do it twice in the pivot function, I don't want to write this code twice, so I'm going
‫to break this out into its own function that I call swap and then we'll pass it the array and the two
‫indexes that we want swapped.
‫And like I said, we will use this in two different places in the pivot function.
‫So now let's start writing Pivot.
‫We will pass this the array, and then we will pass it a pivot index and an end index.
‫And for what we're going to do in this video, the pivot index will be the index of zero and the end
‫index will be the index of six, which is the last item in the array.
‫So this pivot index I'll represent with this arrow and I just put pivot on the arrow, but I like to
‫call the variable name Pivot Index to emphasize that this is an index, not the value of the item at
‫that index.
‫And then we'll say swap index equals pivot index like this.
‫And on the arrow, I'll just write swap.
‫But this is the variable swap index.
‫And then we have a for loop that goes from pivot index plus one to the end index.
‫So I is going to be here.
‫And then in the for loop we'll say if array at the index of I is less than array at pivot index.
‫And in this first iteration of the for loop, the conditional in this if statement is going to be false
‫because six is greater than four.
‫So I'll color that in gray.
‫We'll run the for loop again, which moves I up to here.
‫Now the conditional in the if statement is true.
‫So inside of this, if statement will move swap index up one like that and then we'll run that swap
‫function that we created at the beginning of the video.
‫Like this we pass it the array and swap index and I and it swaps those two items.
‫So if you keep running this code the way we have it here, you'll end up with an array that looks like
‫this.
‫Then we need to run that swap function one more time.
‫We'll pass it the array and pivot index and swap index.
‫And that does this.
‫Now, the only thing left to do is to return the swap index.
‫And I want to emphasize in this case, that will be the index of three.
‫It's not the value of four.
‫That is at that index.
‫We are returning the index.
‫So that is all of our code for pivot.
‫We'll look at this code in a moment in VTS code.
‫And when we do, we'll start with an array that looks like this and then we'll run, pivot on it and
‫we will return that swap index and then look at what the array looks like after we have run.
‫Pivot on that array.
‫So now let's flip over to VS code and take a look at this.
‫So here is our swap function here and I'll scroll up and this is our pivot function here and I'll scroll
‫up again.
‫And with this line we create that array with those seven items in it.
‫And with this line, we get the size of the array.
‫And down here we run, pivot on that array and we pass it the pivot index and the end index.
‫And remember, that pivot is going to return an index and we'll store that in this integer called returned
‫index.
‫And with this line will print out that return index.
‫And with this for loop, we will print out the array.
‫And I'll run this.
‫And you can see up here that our returned index is three.
‫And with the array that's been returned, that four is now in the middle.
‫All of the items that are less than are on the left.
‫All of the items that are greater than are on the right.
‫So it looks like we have a working function for pivot.