• 1 week ago
Transcript
00:00Next, I will talk about this question. It's called Merge Sort on a Link List.
00:04So Merge Sort Sorting is an algorithm. It takes time and log n. We have already used it.
00:11Merge Sort we used it or learned it.
00:15When we studied Divide and Conquer chapter, what was it?
00:20In Recursion. I studied Divide and Conquer and I had sorted an array.
00:25How do you sort the array with Merge Sort?
00:27First, I took a mid-find out of an array. Then I divided the array into two parts.
00:34One was my left half. One was my right half.
00:42What did I do individually in this left half and individually in this right half?
00:48I was sorted. As I was sorted, then I added Merge to Merge.
00:55That means I merge the sorted halves again.
00:59I had a merge function to merge these sorted halves.
01:04I was able to merge these two sorted halves in this way.
01:06Like I take elements here, 5, 4, 3, 2, 1.
01:11Here I will have 3, 4, 5. Here I will have 1, 2.
01:15Now I will merge and I will give them 1, 2, 3, 4, 5.
01:18I apply some process in this process whenever I merge sort.
01:21Now I have seen this process in Arrays or ArrayList.
01:26But now I am going to use this process to sort my linked list.
01:32And visually, diagram wise, if we look at our code and explanation,
01:36then we are doing something like we had done in Arrays.
01:39So if I have to understand MerchSort,
01:43then I have to go into Divide and Conquer.
01:45And if I applied MerchSort in Arrays, I have to understand MerchSort.
01:50If I have to understand MerchSort in Arrays,
01:54then I will not understand MerchSort in Arrays.
01:56But if I don't have to understand MerchSort in Arrays,
01:58then there will be issues.
02:00So first of all, we have to understand MerchSort in Arrays.
02:04How do we apply it to Arrays?
02:06Now let's talk about how we apply it to the link list.
02:08We apply it to MerchSort in 3 parts.
02:12First of all, the first step is to find out the middle of my linked list.
02:17And this is the middle,
02:19like if I take an odd case.
02:24This is the odd case in which I have 3 nodes.
02:26In this case, I know that my middle node will be this.
02:30What is the middle node?
02:31What will happen?
02:31But if I take my even case,
02:34like I have a link list in which there are even number of nodes,
02:38which is n is equal to 4.
02:39This can also be middle,
02:41and this can also be middle.
02:43But I have to take the middle
02:44which is my last node's first half.
02:46So, even if I divide it in 2 parts,
02:49this is the first part and this is the second part.
02:51So I have to take my middle node.
02:54So I am going to write the code in this process.
02:59We can consider another way.
03:01Another way in which I have this node is my middle node.
03:04But I don't write code here.
03:06I will use the middle node in which I will use the middle.
03:09It will always be the last node in the first half.
03:12So this is my first half.
03:14This is my last node.
03:15This is my middle node.
03:17If you want to take this node,
03:19then it will be slightly different code.
03:21We can also write it logically.
03:24I will write this code here.
03:26So what can I say?
03:27When there are odd number of elements in my linked list,
03:30this will be my first half and this will be my second half.
03:34So my middle is my last node.
03:36I don't understand such a small thing.
03:37I don't understand such a small thing.
03:39So the first step of the middle is my first step.
03:43As I have found out my middle node.
03:47As I have found out the middle node,
03:49I have also found out the middle node.
03:49So I have to divide my array in two parts.
03:52So what do I do?
03:53I divide my linked list in two parts.
03:55The first part is my left half of the linked list.
04:00The second part is my right half of the linked list.
04:03Now how do I divide in the left half and right half?
04:06I have found out the mid point.
04:08If I make the next node,
04:12then I will say that my linked list will be broken in two parts.
04:15How?
04:16If I have this linked list,
04:19which has 4 nodes.
04:21What did I do?
04:24I have found out the middle node.
04:25I have found out the middle node.
04:25Now I am saying,
04:26make the next node.
04:28Make the next node.
04:29Make the next node.
04:29It means that
04:30this linked list is pointing out the null value.
04:32and this connection is removed from here.
04:35So this one linked list is this one.
04:38This one linked list is this one.
04:40I will store this node in the right half.
04:45So this is my right half of the linked list.
04:47This is my left half.
04:49So what do I do?
04:50First I have to find out the middle code.
04:52We will discuss the code of middle find out.
04:54Then I have to divide in left half and right half.
04:56How can I divide it?
04:57Mid and next.
04:58Make the next node null.
04:58I will divide the two.
05:00What do I do?
05:01Now I will call the merge sort on the left half.
05:04And the right half.
05:06Now I will merge both sides.
05:09I will merge both sides.
05:10Now I will merge both sides.
05:12For example,
05:13I will add a unit of linked list and another side to the other side.
05:17Now I will merge both sides.
05:20Now we will discuss these 3 steps.
05:23Code wise.
05:24First step is to find out the middle of the linked list.
05:27How do I find out my middle link list?
05:30For this, I have already written a code called Midnode.
05:34Midnode is called Find Midnode or Get Midnode.
05:38Where did I write this?
05:39I wrote this when we were solving the problem of the palindrome.
05:43When I had to check whether my link list is a palindrome or not,
05:47I wrote this question for middle node.
05:50But I will change the code in there.
05:54Before I write that function, if I don't remember,
05:56if you don't remember how to find out the middle node,
06:00then we used the middle node to find out.
06:04We used the slow fast pointer.
06:06I had to initialize the slow head,
06:08I had to initialize the fast as well.
06:11When did I run a loop?
06:14Until my fast is not equal to null,
06:19and until my fast is not equal to null.
06:24I had to run a loop.
06:26And in this loop,
06:27I always increase my slow by 1,
06:31and I increase my fast by 2.
06:35And what did I say in the last?
06:36I said in the last,
06:37that my slow value is my middle node.
06:44I wrote this function in the palindrome question
06:47to find out the middle node of my link list.
06:50But in this question,
06:51I have to change a little bit.
06:53Why do I do this?
06:54If you will see the code
06:56or dry run it on a link list,
06:59then you will realize that
07:01if I take an odd size link list,
07:03I have three nodes in the link list.
07:06In this case,
07:07my function will give me the middle node
07:09which I need to merge sort.
07:11This is the one.
07:12But if I have considered an even case
07:14where I have four nodes in my link list,
07:18then what will I do?
07:19My code will return it as a middle.
07:22Which is wrong.
07:23Now the middle has returned
07:25what I can say is that
07:26the second half node is my first node.
07:27But I don't need the second half node.
07:30I need the last node.
07:33That's why I have to change a little bit here.
07:36What a little bit?
07:37I will initialize the next node here.
07:41I just want to change that little bit.
07:44Then the last node will be my last node.
07:52I will visualize this
07:53on an even size link list.
07:55I will have a link list in which I have 6 nodes.
08:01N is equal to 6.
08:02So what is the second half node?
08:04My slow also is here.
08:05My fast is here.
08:07Then I have first time slow.
08:10I have added fast into this position.
08:13Then I have added slow and here.
08:15Fast I have added this position.
08:17Now the fast will not be able to increase
08:19because there is no next next.
08:22So my loop will be finished here.
08:24and now slow is here.
08:26So what can I say?
08:27If I remove the rest,
08:29what can I say? If this is my slow,
08:31then this will be my mid.
08:33And my mid is the last node
08:35of the first half of my link list.
08:38So in this way,
08:39I will complete my first point,
08:41first step,
08:42to remove the middle of the link list.
08:45So I have to remove the middle of the link list.
08:47You can write it as an algorithm
08:50or in fact, not making notes
08:51when you write the code in the whole merge sort
08:53of link list,
08:54then you can make notes.
08:56So you can write it in the pseudocode form.
08:59What do I have to do next?
09:00I have to perform the next step.
09:02The second step is that
09:04first step,
09:05you have to remove my link list.
09:08So my right,
09:11head,
09:13right head
09:15will become middle
09:18because middle is the last node
09:20first half of the last node
09:22from the last node.
09:24The second half will start
09:25the right half.
09:27So next is equal to right head.
09:29And now I will create null
09:32as I made the next node.
09:32As I made the next node,
09:34I broke my link list in two parts.
09:36I mean,
09:37if my link list looks like this,
09:39where my middle is the middle.
09:41So what do I do?
09:42I will call my right head.
09:46And the middle,
09:47which is next,
09:48I will move here and point
09:53to the null.
09:54So my left half will be this
09:56and my right half will be this.
09:59Now what do I do?
10:00I will call the merge sort.
10:01I will call the merge sort.
10:03I will call my head.
10:05What is the same way?
10:07Head is my left half.
10:10Because if the head starts from here,
10:12this is my head,
10:13where it will be finished,
10:14then it will be finished.
10:16Because it will not be null.
10:18Next, I will call the merge sort.
10:22Which is my right half.
10:25So I will write the code for this.
10:26This becomes my step 2.
10:28So when I completed step 1
10:29and 2 completed,
10:31Now the most important step is
10:33the whole code and the whole question.
10:37Which is to merge both the link lists.
10:39The merge step is the most important
10:40so we have to understand this.
10:42What will I do in the merge step?
10:45In the merge step,
10:46I have to do the same thing.
10:47I have to do with arrays.
10:50What was I do in arrays?
10:51I have to do in arrays.
10:52I have to do a temporary array
10:52which I have to add elements inside.
10:56How do I add?
10:57Ascending order.
10:58So I will explain this as a temporary link list.
11:00I will add a temporary link list.
11:00I will add your elements,
11:01I will add your nodes.
11:03How do I add it?
11:04Ascending order.
11:05So I will explain this before
11:06I will explain it as a diagram wise.
11:08After that, I will write it as a pseudo code.
11:10If I take my left half
11:12returned in sorted form.
11:14And in that,
11:15there are 3 nodes.
11:17There are 5,
11:18there are 9,
11:19there are 11.
11:20And here is my second half.
11:25This is my right half.
11:27In this first,
11:28I have 3.
11:29After that,
11:30I have 8.
11:33In the beginning,
11:35I will have 2 nodes.
11:36One is my left head.
11:38One is my right head.
11:41One is my right head.
11:41What will I do?
11:42First, I will create a temporary link list.
11:45I will name this link list.
11:46I will name this new link list.
11:48Or I can call it merged link list.
11:51Merged link list.
11:53Then I will use a temporary variable.
11:58What will I do with temporary help?
12:00We will traverse the merge link list.
12:02Like this left head and we will add it to the help.
12:05And the right head.
12:07We will add it to the help.
12:07Then with temporary help,
12:09We will add the final temporary link list.
12:11I will add up on my temporary link list.
12:13This is my left head.
12:15This is my right head.
12:16and within the merge link list, the first node I will initialize with minus 1
12:21I assume that in the merge sort, as much data stored in my original link list
12:26it will be greater than zero, I mean positive data
12:29so I will call a node minus 1, so what I can say is that this is my dummy node
12:33dummy node or auxiliary node or helper node
12:37or file to node which I will delete and now I have just made this node
12:42so that I can add more data in it
12:44other than this node, there is no extra use
12:47so what I am doing in the beginning is my dummy node
12:50now my first step is to compare left head data
12:55and right head data
12:57left head data is stored in 5, right head data is 3
13:01I will attach it with temporary next
13:04so what did I do?
13:07temporary next
13:09memory is small
13:11so if it is small, it will go directly to this node
13:14point to this node
13:16then this node will point to this node
13:20but I don't understand the actual memory
13:22I will show temporary
13:25new link list
13:42and it will be temporary
13:43temporary
13:44next
13:45so temporary
13:46here
13:48this node and this node
13:50both are the same
13:50in memory
13:51so it is directly connected to this node
13:53there is no new node
13:54I am pointing to this node
13:56I am pointing to this node
14:11because I have used the right head value
14:12so I will also update it
14:15here
14:15what will compare
14:17now will compare
14:18left head value to right head value
14:195 and 8
14:21small
14:22so temporary
14:23temporary
14:25next
14:25point
14:27and temporary
14:28value
14:29update
14:31temporary
14:31next
14:33so this is not a new node
14:35actually
14:353
14:36point
14:36inside the memory
14:38temporary
14:39now I am pointing to this node
14:40so my left head will also move
14:42further
14:43left head will move
14:44further
14:45now compare
14:46left head value
14:47and right head value
14:49small
14:49small
14:50small
14:51temporary
14:52next
14:52point
14:54temporary
14:56next
14:56temporary
14:57next
14:58with this
14:58right head
15:01point
15:01as I am pointing to this node
15:03so I will add it
15:07to this node
15:08to this node
15:08I will add it
15:09right head
15:09now I will not have to compare
15:12to this node
15:13because I have added the right
15:14half of the linked list
15:15I have added the merge
15:17linked list
15:18in the left
15:19half
15:20all the nodes
15:21of the nodes
15:21I will add
15:22to this node
15:24temporary
15:25next
15:25to 9
15:27and
15:28temporary
15:28will come here
15:30as I am
15:32temporary
15:32here
15:33I have to update the left
15:33half
15:34left half
15:36update
15:37left head
15:38will come here
15:39again
15:40I will add another node
15:41next
15:42point
15:43temporary
15:44which is my
15:4511
15:45temporary
15:46update
15:47this node
15:48will come here
15:48up
15:51and
15:52my left
15:52head
15:52will become
15:56null
15:56so I have completely
15:59traversed this
16:00and this
16:00linked list
16:01completely
16:02traversed
16:02I have a merged
16:04linked list
16:05in both
16:06complete
16:06but
16:07this
16:08merged linked list
16:18will become
16:19my final
16:21linked list
16:21or the final
16:22head of my
16:23linked list
16:24this will be
16:25merged linked list
16:27next
16:28if the merged linked list
16:30is the head
16:30of this node
16:31then
16:33next
16:33will start
16:35this node
16:36delete
16:37inside this node
16:39this node
16:39my final
16:40head
16:40which
16:41is sorted
16:44order
16:44arranged
16:45so
16:46how will
16:46this code
16:47will be
16:47like
16:47I wrote
16:49in merge
16:50when I was
16:52talking about
16:53first
16:53I have a left
16:55head
16:56or
16:56head
16:571
17:01which is my
17:02left
17:02head
17:022
17:03linked list
17:051
17:05and linked list
17:06now
17:06to merge
17:07first
17:08first
17:08I will
17:09make a new
17:09linked list
17:10or
17:10I will make a new
17:10node
17:11merged
17:12linked list
17:13which
17:14will be
17:15new
17:15node
17:16which
17:16will be
17:17temporary
17:20and
17:21I will initialize
17:23merged linked list
17:24I hope
17:25we can imagine
17:26that
17:26here
17:27I have
17:27a temporary
17:27variable
17:31which
17:31points
17:32now
17:33I have to
17:35run a loop
17:35this loop
17:36will
17:37head
17:391
17:39null
17:41or
17:43head
17:442
17:44null
17:45equal
17:45until
17:46head
17:471
17:48compare
17:48head
17:492
17:49data
17:51I will add
17:52to the
17:52linked list
17:53and then
17:54I will update
17:55the
17:56head
17:572
17:58loops
17:59until
17:59head
17:591
18:00is
18:00null
18:01when
18:01the
18:02is
18:04here
18:05I will return
18:08Merged
18:09linked list
18:10next
18:10because
18:12this is
18:12the
18:12node
18:13is
18:13my dummy
18:15node
18:16I will delete
18:18and return
18:20Merged
18:20linked list
18:20next
18:21in
18:213
18:22steps
18:25to find
18:25middle
18:26of
18:26the
18:26linked list
18:27second
18:27is
18:28to divide
18:28the
18:29linked list
18:29into
18:29two
18:29halves
18:30then
18:30I will call
18:33Merged
18:34and the
18:36most important
18:37step
18:37is
18:37to merge
18:38temporary
18:39merged
18:40linked list
18:41which will traverse
18:43linked list
18:44and
18:44now
18:47I will write
18:47Merged
18:48sort
18:48code
18:49write
18:49code
18:49Así
18:49you