• 1 week ago
Transcript
00:01First of all, I will write a function as public void or return type as node
00:08Because it will return its final, which will be sorted and the head will return
00:15Public node, MergeSort
00:17Because MergeSort is a recursive function, there are some parameters to track the recursion
00:22So I will pass the parameters as my head
00:26In the beginning, the first step that I have to perform is to find mid
00:32After that, I have to perform the next step
00:34I will call the MergeSort and then I will perform the Merge step
00:42I will create a function that will give my mid
00:47I will name it GetMid
00:53When I have mid, what do I have to do?
00:56After that, my head will be the right half
00:59That will be mid
01:01Next
01:04And mid
01:05Next becomes equal to null
01:08Now I will call the MergeSort for my head
01:11Which is my left half
01:12And I will call the MergeSort for my right head
01:16Which is my right half
01:18Now I will return the Sorted Link List
01:19Now I will return the Sorted Link List
01:20I will store it in my left
01:24Or new left
01:27And I will store it in my right
01:30And I will store it in my new right
01:31Now I will call both
01:32I will call the Merge function
01:35Merge for new left
01:36And merge for new right
01:39Now the Merge function
01:40The head will return the Sorted Link List
01:43I will do it directly
01:46Now I have to complete this function
01:49Which is my GetMid function
01:50And I have to complete my Merge function
01:52With the MergeSort
01:54The base case
01:55When it comes to me
01:56When my link list is empty
01:57Or only one single node
01:59When the link list is empty
02:01Then it is already sorted
02:02And when it is single node
02:03Then it is already sorted
02:05So when the head value is null
02:07Or the next value is null
02:10Then the next value is null
02:10The link list is already sorted
02:13So here I will return my head
02:15Now I will write my GetMid function
02:18For GetMid function
02:20I will call public node
02:23In fact this function
02:25I can keep the private type
02:25If I don't have access outside
02:28Private node
02:33GetMid
02:33I need my head
02:36So I will pass my head
02:40Node slow is equal to head
02:42Node fast is equal to head
02:45Next
02:46I have to run a loop
02:47Until my fast is not equal to null
02:52And until my fast next is also not equal to null
02:58Every time I will update my slow
02:59By one
03:00And I will update my fast
03:02By one
03:02And I will update my fast
03:03By one
03:04By two
03:06Last
03:06I will return my slow
03:08Because my slow
03:09What will become my slow
03:10My slow is my mid node
03:13This I have completed
03:14GetMid
03:14I will complete my merge function
03:17Now I will complete my merge function
03:18Merge function
03:19I will write private
03:23Node
03:25Merge
03:26Merge
03:35Merge
03:36Merge
03:37Merge
03:38Merge
03:38Merge
03:38Merge
03:38Merge
03:39Merge
03:40Merge
03:41Merge
03:41Merge
03:42Merge
03:42Merge
03:42Merge
03:43Merge
03:44Merge
03:45Merge
03:48Merge
03:49Merge
03:50Merge
03:51Merge
03:51Merge
03:51Merge
03:51Merge
03:52Merge
03:53Merge
03:54Merge
03:55and until my head 2 is not equal to null.
04:01Until both are equal to my null,
04:03what do I have to do?
04:04I will compare that if my head 1 data is less than equal to my head 2 data.
04:10In that case, what do I do?
04:12I will point to temporary next to my head 1
04:16because I will point to temporary next to head 1
04:17because there is a small value.
04:20Head 1 will be here.
04:22Now I will update head 1 to head 1 next
04:27and I will update temporary to temporary next.
04:31This scenario will be this.
04:32In which my temporary next is now pointing to my head 2.
04:38I will update head 2 to next and I will do temporary next.
04:46Whatever the value is small in head 1 and head 2 data,
04:49I have created temporary next.
04:50I have created next.
04:51Then I will update the pointer of that and I will update temporary.
04:55In last, I will run two loops.
04:57For the rest of the elements,
04:58that until head 1 is not equal to null,
05:02until temporary is now pointing to head 1.
05:06In fact, this will be temporary next is now pointing to.
05:10and these two statements will be copied again.
05:16In the same manner, head 2 will also run a loop.
05:19Until my head 2 value is not equal to null,
05:23until I will perform these three actions.
05:29What will I return to the last one?
05:30I will return to the last one
05:38and I will delete my dummy node.
05:40That's why I will return it to next.
05:43Let's do save.
05:44Now I have written a code for merge sort.
05:48I have written a code for merge sort.
05:51I have written a code for merge.
05:51and I have written a code for get mid.
05:54Here, in merge sort,
05:57I am going to write another condition.
06:01Here, there will not be another condition.
06:02There will be another condition.
06:03It will be either this condition,
06:05that my link list will be removed,
06:06or it will be single load.
06:08Now I will create a link list in this function.
06:11Link list.
06:13I will create it from scratch.
06:17This is not my Java collection framework.
06:19This is my scratch link list.
06:21So, in the link list,
06:23I will do first in add first,
06:261,
06:26then add first,
06:302,
06:31then add first,
06:343,
06:38add first,
06:394,
06:40and add first,
06:42and add first,
06:425.
06:43So, my link list will be like,
06:455,
06:46then 4,
06:47then 3,
06:48then 2,
06:48then 1.
06:50So, this is reverse sorted.
06:51I have to sort it in ascending order.
06:53So, I will print it first.
06:55So, I will print it first.
06:56The state of my link list will be printed.
06:59After that,
07:00I will call my merge sort.
07:02I will call my merge sort.
07:03If I will call your link list.head.
07:06Then,
07:07and then,
07:08the new head return to sorted list will be stored in my link list.head.
07:14I will call my merge sort.
07:15I will use the merge sort.
07:20Then,
07:22I will print it again.
07:24Then,
07:24I will print it again.
07:24Let's do this.
07:25Now,
07:26run.
07:29I will print it again.
07:29I have the first link list.
07:31The first link list is printed.
07:32It is 5,
07:324,
07:333,
07:332,
07:331.
07:35After that,
07:35the link list has not changed.
07:36Why not changed?
07:37We will look at our code.
07:39I will print it again.
07:39In the merge sort,
07:41we have a small mistake here.
07:43I have written the next head.
07:45That is not equal to null.
07:46So,
07:47whenever a single node exists in the linked list,
07:49then,
07:49the condition will be true.
07:52In that case,
07:53I will return the head directly.
07:54So,
07:55the place of not equal to,
07:56the place of equal to equal to,
07:58was equal to equal to.
07:58So,
07:59because of this small mistake,
08:00the merge sort function has not run.
08:02Once we have checked,
08:04the link list is not left.
08:06So,
08:06I will not do that.
08:07I will do that again.
08:09Now,
08:09I will save it again.
08:09And now,
08:10run.
08:12What happened?
08:13In the beginning,
08:13my link list was 5,
08:144,
08:153,
08:152,
08:151.
08:15Now,
08:15my link list is sorted.
08:171,
08:182, 3,
08:184,
08:19In fact,
08:19here,
08:20if I remove 5,
08:21I will use an even size link list.
08:23So,
08:24what will happen?
08:24In the beginning,
08:254,
08:263,
08:262,
08:261.
08:26Now,
08:271,
08:272,
08:283,
08:284.
08:281,
08:281,
08:282,
08:293,
08:294.
08:291,
08:382,
08:383,
08:382,
08:382,
08:381,
08:393,