﻿1
00:00:00,490 --> 00:00:04,810
‫So now we're going to write our code for our pivot helper function.

2
00:00:05,170 --> 00:00:10,690
‫We're going to run it on this array, which I will shrink down into the corner.

3
00:00:11,110 --> 00:00:17,080
‫And the first thing I want to talk about with Pivot is that there are two different places where we

4
00:00:17,080 --> 00:00:23,170
‫swap items in the array, and we can do that with something like this.

5
00:00:23,170 --> 00:00:30,400
‫But because we do it twice in the pivot function, I don't want to write this code twice, so I'm going

6
00:00:30,400 --> 00:00:37,840
‫to break this out into its own function that I call swap and then we'll pass it the array and the two

7
00:00:37,840 --> 00:00:40,450
‫indexes that we want swapped.

8
00:00:40,810 --> 00:00:46,210
‫And like I said, we will use this in two different places in the pivot function.

9
00:00:46,660 --> 00:00:49,450
‫So now let's start writing Pivot.

10
00:00:49,750 --> 00:00:57,820
‫We will pass this the array, and then we will pass it a pivot index and an end index.

11
00:00:58,180 --> 00:01:04,780
‫And for what we're going to do in this video, the pivot index will be the index of zero and the end

12
00:01:04,780 --> 00:01:09,610
‫index will be the index of six, which is the last item in the array.

13
00:01:10,030 --> 00:01:17,890
‫So this pivot index I'll represent with this arrow and I just put pivot on the arrow, but I like to

14
00:01:17,890 --> 00:01:24,820
‫call the variable name Pivot Index to emphasize that this is an index, not the value of the item at

15
00:01:24,820 --> 00:01:25,930
‫that index.

16
00:01:26,470 --> 00:01:32,860
‫And then we'll say swap index equals pivot index like this.

17
00:01:33,100 --> 00:01:35,740
‫And on the arrow, I'll just write swap.

18
00:01:35,740 --> 00:01:38,920
‫But this is the variable swap index.

19
00:01:39,490 --> 00:01:46,840
‫And then we have a for loop that goes from pivot index plus one to the end index.

20
00:01:46,840 --> 00:01:50,110
‫So I is going to be here.

21
00:01:50,590 --> 00:01:59,350
‫And then in the for loop we'll say if array at the index of I is less than array at pivot index.

22
00:01:59,680 --> 00:02:05,770
‫And in this first iteration of the for loop, the conditional in this if statement is going to be false

23
00:02:05,770 --> 00:02:08,380
‫because six is greater than four.

24
00:02:08,890 --> 00:02:11,320
‫So I'll color that in gray.

25
00:02:11,650 --> 00:02:15,640
‫We'll run the for loop again, which moves I up to here.

26
00:02:15,760 --> 00:02:19,480
‫Now the conditional in the if statement is true.

27
00:02:19,750 --> 00:02:29,530
‫So inside of this, if statement will move swap index up one like that and then we'll run that swap

28
00:02:29,530 --> 00:02:33,020
‫function that we created at the beginning of the video.

29
00:02:33,040 --> 00:02:41,290
‫Like this we pass it the array and swap index and I and it swaps those two items.

30
00:02:41,860 --> 00:02:48,850
‫So if you keep running this code the way we have it here, you'll end up with an array that looks like

31
00:02:48,850 --> 00:02:49,720
‫this.

32
00:02:50,200 --> 00:02:53,770
‫Then we need to run that swap function one more time.

33
00:02:53,770 --> 00:02:57,910
‫We'll pass it the array and pivot index and swap index.

34
00:02:57,910 --> 00:03:00,220
‫And that does this.

35
00:03:00,760 --> 00:03:05,710
‫Now, the only thing left to do is to return the swap index.

36
00:03:06,250 --> 00:03:11,140
‫And I want to emphasize in this case, that will be the index of three.

37
00:03:11,170 --> 00:03:13,870
‫It's not the value of four.

38
00:03:13,900 --> 00:03:15,280
‫That is at that index.

39
00:03:15,280 --> 00:03:17,770
‫We are returning the index.

40
00:03:18,280 --> 00:03:21,370
‫So that is all of our code for pivot.

41
00:03:21,580 --> 00:03:24,550
‫We'll look at this code in a moment in VTS code.

42
00:03:24,550 --> 00:03:31,120
‫And when we do, we'll start with an array that looks like this and then we'll run, pivot on it and

43
00:03:31,120 --> 00:03:37,660
‫we will return that swap index and then look at what the array looks like after we have run.

44
00:03:37,660 --> 00:03:39,640
‫Pivot on that array.

45
00:03:40,210 --> 00:03:44,290
‫So now let's flip over to VS code and take a look at this.

46
00:03:44,890 --> 00:03:53,740
‫So here is our swap function here and I'll scroll up and this is our pivot function here and I'll scroll

47
00:03:53,740 --> 00:03:54,820
‫up again.

48
00:03:55,660 --> 00:04:00,010
‫And with this line we create that array with those seven items in it.

49
00:04:00,310 --> 00:04:03,520
‫And with this line, we get the size of the array.

50
00:04:03,940 --> 00:04:10,780
‫And down here we run, pivot on that array and we pass it the pivot index and the end index.

51
00:04:11,110 --> 00:04:17,950
‫And remember, that pivot is going to return an index and we'll store that in this integer called returned

52
00:04:17,950 --> 00:04:18,880
‫index.

53
00:04:19,180 --> 00:04:23,110
‫And with this line will print out that return index.

54
00:04:23,260 --> 00:04:27,010
‫And with this for loop, we will print out the array.

55
00:04:27,340 --> 00:04:28,600
‫And I'll run this.

56
00:04:29,020 --> 00:04:33,100
‫And you can see up here that our returned index is three.

57
00:04:33,340 --> 00:04:37,810
‫And with the array that's been returned, that four is now in the middle.

58
00:04:38,080 --> 00:04:41,250
‫All of the items that are less than are on the left.

59
00:04:41,260 --> 00:04:44,560
‫All of the items that are greater than are on the right.

60
00:04:45,130 --> 00:04:49,480
‫So it looks like we have a working function for pivot.

