﻿1
00:00:00,580 --> 00:00:02,380
‫So now we're going to do our pivot.

2
00:00:02,740 --> 00:00:03,280
‫Intro.

3
00:00:03,280 --> 00:00:09,370
‫Pivot is going to be a helper function much like the merge function was a helper function for merge

4
00:00:09,370 --> 00:00:13,510
‫sort pivot is going to be a helper function for quicksort.

5
00:00:14,110 --> 00:00:17,530
‫So I'm going to bring up an array here and do it like this.

6
00:00:17,530 --> 00:00:24,940
‫And what the pivot function is going to do is this We're going to have our pivot item and we're going

7
00:00:24,940 --> 00:00:26,410
‫to highlight that for there.

8
00:00:26,800 --> 00:00:32,050
‫It's going to sort all of these and do this swap like that.

9
00:00:32,230 --> 00:00:33,790
‫That's all it's going to do.

10
00:00:34,450 --> 00:00:40,420
‫So let's put this back like this and go through the steps that we need to go through to make that happen.

11
00:00:41,130 --> 00:00:46,710
‫So I'm going to go into a little bit more detail in this intro video than I normally do in an intro

12
00:00:46,710 --> 00:00:52,140
‫video, because it's going to be difficult to explain without bringing in the variables.

13
00:00:52,650 --> 00:01:00,260
‫So we have our pivot point here, and then we're going to loop through this from the next item over

14
00:01:00,270 --> 00:01:07,350
‫that's going to be I in our loop, we're going to have a variable called pivot that points to this item

15
00:01:07,650 --> 00:01:15,270
‫and another one called swap that's going to start out being set to pivot, but this one will move.

16
00:01:15,780 --> 00:01:21,480
‫So we start out by looking at that first item that I is pointing to there, the six.

17
00:01:21,480 --> 00:01:25,710
‫It is greater than and we'll move I to the next item.

18
00:01:26,310 --> 00:01:33,210
‫Any time you have something that is less than the pivot variable, we will move the swap up to here.

19
00:01:33,950 --> 00:01:39,620
‫And then we will use that to swap these two items and then I will move forward.

20
00:01:39,620 --> 00:01:42,570
‫That one's greater than and this one's less than.

21
00:01:42,590 --> 00:01:45,290
‫So we move swap up first.

22
00:01:46,160 --> 00:01:48,830
‫And that we swap these two values.

23
00:01:49,160 --> 00:01:50,420
‫Same thing here.

24
00:01:50,960 --> 00:01:52,090
‫Swap moves up.

25
00:01:52,100 --> 00:01:53,870
‫We swap these two values.

26
00:01:54,590 --> 00:01:56,510
‫Then we move I to the last item.

27
00:01:56,510 --> 00:01:59,360
‫It's greater than and we are now done with that loop.

28
00:01:59,360 --> 00:02:01,880
‫So I will remove that arrow.

29
00:02:02,450 --> 00:02:09,320
‫And the last thing we need to do is swap these two items and we're done with the pivot variable.

30
00:02:09,800 --> 00:02:15,830
‫And normally when we're working with an array at the end of the method or the function or whatever we're

31
00:02:15,830 --> 00:02:17,540
‫doing, we return the array.

32
00:02:17,930 --> 00:02:21,080
‫But that is not what we're going to do here.

33
00:02:21,530 --> 00:02:25,400
‫We need to return that swap variable.

34
00:02:25,430 --> 00:02:28,640
‫Now, this is everything the pivot function's going to do.

35
00:02:28,640 --> 00:02:34,670
‫But I'm going to show you why we need to have that swap variable, because when we run quicksort on

36
00:02:34,670 --> 00:02:41,780
‫this side, we are going to be running this on the index of zero through swap minus one.

37
00:02:42,350 --> 00:02:49,490
‫When we run it on this side, we're going to run it on swap plus one to the end of the array.

38
00:02:49,700 --> 00:02:53,990
‫So that's why we have to know what that index is.

39
00:02:55,350 --> 00:02:58,710
‫And that is our overview of Pivot.

