I'm very weak in algorithms. From watching your video, the tutorial is very easy to understand. I understand how merge sort works. Thank you very much from the Vietnamese guy !
@CodingWithJohn3 жыл бұрын
Thanks Vietnamese Guy! Very glad I was able to help!
@akosdanielbollok2 жыл бұрын
This is pure gold! I wish you did a full Data structures&algorithms course, that would be awesome.
@metdelta13 жыл бұрын
Perhaps the BEST explanation of Merge Sort out there. 👍🏻 The code along with variable names, justifies the natural thinking of the human mind. Thanks a lot ❤️ Subscribed ✅ Best wishes and hope your channel gets all the love and support it deserves.
@marvinzelaya11452 жыл бұрын
Agreed, he explained it clearly and step by step, such a great explanation!!!
@sadekjn Жыл бұрын
Professors at top CS programs like UC Berkeley (where I suffered) aren't able to explain this stuff as well and as clearly as you in multiple 2-hour-long lectures. From the bottom of my heart, I thank you for existing.
@moeal51103 жыл бұрын
I watched and read neumors explanations about this but only after watching your video I was able to understand each line and now I can see it in my brain's eyes. I also dreamt about it. Thanks a lot! Please do more
@taldennis80582 жыл бұрын
This video is something else, I watched all the big channels before I got here, this explanation is by far the most detailed and the most accurate, also providing an explanation for all the edge cases. the implementation is super clear and clean. this channel is underrated, I wish I knew this channel earlier. thank you so much, you're the best!!
@TuanBuianonymous2 жыл бұрын
agree
@pranusharavula29432 жыл бұрын
Agreed yeah
@timjoyalle3182 жыл бұрын
I had to watch more than once to really absorb the info. It still is a lot less time and money than it costs me to listen to lectures at my university. Thank you for taking the time to make this video!
@AceNinja11013 жыл бұрын
Very informative video! Really could have used this back when I was in college lol. Also just attempted to run this with 1 billion elements and took about 8 and half minutes using C# for anyone who's curious!
@davudazizov49664 ай бұрын
I have to tell this, I've been trying to understand merge sort for quite a few days, and this video is the best I've found on KZbin! Thank you for explaining this in a very easy way!
@cr84442 жыл бұрын
I can't thank you enough for this clean explanation. I haven't been able to understand this algorithm until I stumbled on your video. Now I practically know what each line of code means. You're a genius!!
@elshaterhassansaber76443 жыл бұрын
I've been searching for a good explanation and a good clean code but luckily !! i found both in one video you're an amazing guy
@jaydeeppawar17202 жыл бұрын
true a clean code is a must for understanding ....
@LizyAdАй бұрын
Out of every video I have watched on Merge Sort, you broke it down the best. I really appreciate you John. Thank you so much.
@Mr.Grave.3 ай бұрын
Career jumping here. Really appreciate your work to provide these great tutorials for free.
@akshaypatel99823 жыл бұрын
Wow, the first explanation that made complete sense with 0 confusion.Thank you very much! Wish you were my teacher back in college lol
@TuanBuianonymous2 жыл бұрын
my english is not good to understand all words you said but when i look into your code i absolutely understand all, thank you very much.
@timurboltaev86882 жыл бұрын
Thank you. Watched the video. Went to the code. Copied it down with a pen on paper to understand better. Rewatched the video. I think I have grasped it.
@Yersinia19882 жыл бұрын
I was so frustrated and sad about not understanding the merge sort. Your explnation is very clear, made my day. Thanks a lot! :)
@neurohan2 жыл бұрын
You are, without a doubt, one of the most talented educators I've come across (both online and offline).
@hive_san6152 ай бұрын
I just started DSA recently and was stuck at merge sort for two days, understood the concept but not why code was written the way it was. You cleared all the doubts thanks you very much.
@raoulkid2 жыл бұрын
The only person on yt that actually explained the merge subroutine properly, thanks, +1 sub
@AliBulut-tg6bh9 ай бұрын
We all have been taught that merge sort is a fast efficient but memory poor algorithm. But to actually see that happen really changes things. I think it was the first time I saw someone pushing the algorithm to its limit memory-wise. Thank you sir, it was a great explanation.
@kosaken12072 жыл бұрын
Absolutely brilliant explanation! I was so confused for a school assignment on why the last two while loops exist in the code. Turns out it was just cleanup!
@sujanreddy97702 жыл бұрын
Whenever I need to refer algorithms I will directly watch your videos thanks for explaining so clearly.
@HisExcellencyAKK2 жыл бұрын
Great work! possibly memory can be improved a bit by passing indices (start, mid, end) into split subroutine instead of sub arrays. We would only need an auxiliary space of size n at merge subroutine. Since merge subroutine happens after split subroutines, the maximum auxiliary space that will be needed is n instead of n*log(n).
@natnal9587 Жыл бұрын
Honestly, this is the best video I have ever seen about merge sort.
@sekharsamanta6266 Жыл бұрын
Hi Mr. John, I've seen so many videos on different algorithms of other people but your explanation is crystal clear and unique
@franklinoduro72742 жыл бұрын
Hands down this is the best sorting algorithm video i have seen on youtube. Thanks John
@J-wm4ss2 жыл бұрын
Thanks! This makes SO MUCH MORE SENSE than other explanations!
@sajanarora2452 жыл бұрын
Gave the link to this in the comments of every video I watched to understand merge sort because this is the best explanation of Merge sort. Thanks a lot man!!! Subscribed
@wasekasi2 жыл бұрын
This is by far the best sorting app I've seen someone code. Awesome.
@RanVargas5 ай бұрын
Hey Man, I just wanted to comment and say I really thank you, I hadn't fully understood Merge Sort at least on the Merge step, but now I am kind of like grasping it, thank you for explaining with time and detail.
@haidaralihammoud2686 Жыл бұрын
That is by far the best explanation out there! Amazing technique of teaching and breaking it down. Thanks a lot!
@Nerdymelreads Жыл бұрын
You do not know how much I looked around for a better explanation. You nailed it. Earned a new subbie.
@HelicopterRidesForCommunists2 жыл бұрын
You are such a good teacher. Very talented at communicating information and concepts. 1 million sorted thanks to you.
@annkit872 жыл бұрын
Thanks for the great explanation @Coding with John. I tried to make the program more detailed with step by step recursive calls and some visualization in source code & output. Implementation with example explanation & tried to visualize every recursive call until it returns back the control to the calling function. PS: Please do not confuse with code written for printing left & right parts at specific levels in the recursive calls, that is only for visualization the flow for better understanding. private static void mergeSort() { int[] elements = {22, 14, -7, 56, 3}; System.out.println("Input Array: " + Arrays.toString(elements)); // Split array into 2 parts until we get single sorted element // Merge every subset of elements using temp sorted array performMergeSort(elements, 0, elements.length - 1, 1); System.out.println("Sorted Array: " + Arrays.toString(elements)); } private static void performMergeSort(int[] elements, int start, int end, int levels) { // Follow the steps below the solve the problem: // MergeSort(arr[], l, r) // If l < r // Find the middle point to divide the array into two halves: // middle m = l + (r - l)/2 // Call mergeSort for first half: // Call mergeSort(arr, l, m) // Call mergeSort for second half: // Call mergeSort(arr, m + 1, r) // Merge the two halves sorted in steps 2 and 3: // Call merge(arr, l, m, r) if (start < end) { int mid = start + (end - start) / 2; // split elements into 2 parts - left & right // recursively do this until we get reach at single element for(int l =0; l < levels; l++){ System.out.print("\t"); } System.out.println("START: " + start + ", MID: " + mid + ", END: " + end + ", LEVEL: " + levels); performMergeSort(elements, start, mid, levels+1); performMergeSort(elements, mid + 1, end, levels+1); // merge back left + right elements after comparing their values merge(elements, start, end, mid, levels); } // 22, 14, -7, 56, 3 // legends: start => s, end => e // (s < e) == (0 < 4) == true // 0 + (4 - 0)/2 = 2 = mid // left = performMergeSort(elements, 0, 2); 22, 14, -7 // (s < e) == (0 < 2) == true // 0 + (2-0)/2 = 1 = mid // left = performMergeSort(elements, 0, 1); 22, 14 // (s < e) == (0 < 1) == true // 0 + (1 - 0)/2 = 0 = mid // left = performMergeSort(elements, 0, 0); 22 // (s < e) == (0 < 0) == false => return; // right = performMergeSort(elements, 1, 1); 14 // (s < e) == (1 < 1) == false => return; // merge = left + right (while sorting) = merge (elements, 0, 1, 0) // right = performMergeSort(elements, 2, 2); -7 // (s < e) == (2 < 2) == false => return; // merge = left + right (while sorting) = merge (elements, 0, 2, 1) // right = performMergeSort(elements, 3, 4); 56, 3 // (s < e) == (3 < 4) == true // 3 + (4-3)/2 = 3 = mid // left = performMergeSort(elements, 3, 3); 56 // (s < e) == (3 < 3) == false => return; // right = performMergeSort(elements, 4, 4); 3 // (s < e) == (4 < 4) == false => return; // merge = left + right (while sorting) = merge (elements, 3, 4, 3) // merge = left + right (while sorting) = merge (elements, 0, 4, 2) } private static void merge(int[] elements, int start, int end, int mid, int levels){ // declare 2 temp arrays in every merge call // (elements, 0, 1, 0) // mid - start size + 1 (0 - 0 + 1) = 1 // end - mid size (1 - 0) = 1 // left[] = 0 -> 22 // right[] = 1 -> 14 // (elements, 0, 2, 1) // mid - start size + 1 (1 - 0 + 1) = 2 // end - mid size (2 - 1) = 1 // left[] = 0 -> 14, 1 -> 22 // right[] = 2 -> -7 // (elements, 3, 4, 3) // mid - start size + 1 (3 - 3 + 1) = 1 // end - mid size (4 - 3) = 1 // left[] = 0 -> 56 // right[] = 1 -> 3 // (elements, 0, 4, 2) // mid - start size + 1 (2 - 0 + 1) = 3 // end - mid size (4 - 2) = 2 // left[] = 0 -> -7, 1 -> 14, 2 -> 22 // right[] = 0 -> 3, 1 -> 56 int[] left = new int[mid - start + 1]; int[] right = new int[end - mid]; for(int l =0; l < levels+3 ; l++){ System.out.print("\t"); } System.out.print("LEVEL: " + levels+" "); for(int leftCounter = 0; leftCounter < left.length; leftCounter++){ left[leftCounter] = elements[start + leftCounter]; for(int l =0; l < levels+3 ; l++){ System.out.print("\t"); } System.out.println("LEFT PART: " + elements[start + leftCounter]); } for(int l =0; l < levels + 3; l++){ System.out.print("\t"); } System.out.println("Left Array: " + Arrays.toString(left)); for(int rightCounter = 0; rightCounter < right.length; rightCounter++){ right[rightCounter] = elements[mid + rightCounter + 1]; for(int l =0; l < levels + 3; l++){ System.out.print("\t"); } System.out.println("RIGHT PART: " + elements[mid + rightCounter + 1]); } for(int l =0; l < levels + 3; l++){ System.out.print("\t"); } System.out.println("Right Array: " + Arrays.toString(right)); // int[] merged = new int[left.length + right.length]; int i = 0, j = 0 ,k = start; // declared merged array of size = left + right = 2 while ( i < left.length && j < right.length) { if (left[i] < right[j]) { elements[k] = left[i]; i++; k++; } else { elements[k] = right[j]; j++; k++; } } // copy remaining already sported elements in merged array from left array while ( i < left.length) { elements[k] = left[i]; i++; k++; } // copy remaining already sorted elements in merged array from right array while (j < right.length){ elements[k] = right[j]; j++; k++; } }
@mahmoudmoussa67223 жыл бұрын
For such comprehensive&best merge sort out there, you won a subscriber. Thanks.
@sagarrawal77402 жыл бұрын
seriously one of the best merge sort explanation
@abhishekhm12642 жыл бұрын
I cannot believe this channel exists. The way he explains!!!. Just Awesome!!.
@igor99198 ай бұрын
This is by far the best tutorial for merge sort, thanks so much
@LTZMSYAL2 жыл бұрын
John you did an awesome job in this video! You're the best course I could find on the merge sort algorithm. I really want to thank you from France for that. Keep up the good work :)
@nosehad5486 Жыл бұрын
great, you are the only one that explained it in a way that i understand
@mukeshprakash811210 ай бұрын
Wouldn't be able to get a more clear explanation for Merge Sort and at the same time can't believe Johnny Sins became a programmer as well lol(no offense. keep up the good work man
@ryandailey14962 жыл бұрын
You said the equals in the "if (leftHalf[i]
@tanvirkazirocks11 ай бұрын
f (leftHalf[i]
@doncilaarcadie2 жыл бұрын
Finally I got this!!! Thank you!!! The best explanation so far!!!
@chrismendez11773 жыл бұрын
Seriously I love the content you make, its made my learning much more better. You're so relaxed and calm about the way you explain your code. Good job!!
@eduardohenriquedeassis9729 Жыл бұрын
Hey Bro, it is for sure, one of the best tutorial about this subject I ever saw, I was struggling to understand it, but now it´s clearwater!!!!! Thanks for that. Greetings from Brazil.
@aparajithasaravanakumar7998 Жыл бұрын
OMG, You are the best teacher I have ever seen
@sheriffcrandyАй бұрын
Thanks bro this helped me a lot
@kushalmondal6182 жыл бұрын
With this Explanation any one can write Merge sort on their first Program , Subscribed quickly , Never gonna miss the chance to get better explanations of Hard topics, Like your work........💯🔥
@HelicopterRidesForCommunists2 жыл бұрын
My computer can likely handle the billion ints. This was so great. Please continue making tutorials!
@__nitinkumar__2 жыл бұрын
Hey John, You are probably the test instructor I have ever seen. I used to get scared of Merge Sort just by looking at it's algorithm. Now seeing the implementation, it's actually pretty easy. Thanks for making it look so simple and writing as well.
@kamdave18954 ай бұрын
only moved up to 100 million before I ran out of memory. However thanks for taking us through this algorithm step by step. It made the quick sort algorithm easier to understand.
@onestopautomationmerasaab50983 жыл бұрын
Please please do make videos on all algorithms. Its the best and non confusing explanation ever. Cheers mate.
@mir.98052 жыл бұрын
I can't believe I've watched this entire video and without being bored. I can't even sit in class for 15 minutes without going to sleep and I'm literally watching this at 2:32am in bed.
@mesbahied42843 ай бұрын
2:11 HHHHHHHHH
@jacobkreifels76902 жыл бұрын
These sorts always made no sense but you made it super simple
@hawagiri403510 ай бұрын
easiest explanation ever, keep up the good work.
@rishijuvekar75723 жыл бұрын
What an excellent explanation !! Simple, clear and concise. Thank you very much.
@celinareger2704 Жыл бұрын
I really like your explanation. I watch many other videos befor and as a biginner I understand nothing but your examples are really good and understandable. 😊 Also you're make it really interesting.
@kevalkrishna41342 жыл бұрын
Such a calm and beautiful explanation for merge sort algo , loved it. And yeah ,you earned a new subscriber.
@juanrada19402 жыл бұрын
Fantastic video! "If you want to be cool you can..." is priceless!
@sagarkalburgi22 күн бұрын
This was so easy to understand, no wonder you have so many views on your videos. Thanks!
@hehhehdummy2 жыл бұрын
Best lecture on the subject I've come across. Love the demo at the end too. I know there's a way to write this algorithm with 1 helper array (instead of creating new arrays all the time), it seems not to have mattered too much.
@sriplano748 Жыл бұрын
Thanks John for a real good explanation of Merge Sort algorithm with code.
@lyn89642 жыл бұрын
Your videos let me fall in love with java! So much Java fun!
@MrRobschke3 жыл бұрын
This video is insanely well made!! Good job :)
@pelinegriboyun9482 жыл бұрын
awesome video! couldn't understand merge sort at all before this explanation, thank you so much
@pontierent7343 жыл бұрын
I know its not really that important, but for copying the left and right, I think it would make it more understandable to loop from the length of the respective arrays, and for the right half just add the midpoint index to the original arrays index.
@CodingWithJohn3 жыл бұрын
Thanks, I like the idea! I'm always looking for ways to make my code as clean as possible. When preparing this video I saw some implementations that took the length of each array as additional parameters instead of just looking at the array, which I thought was ridiculous. I also saw some that incremented indexes on the same line as doing assignments (leftHalf[i++] = inputArray[k++], etc), which works fine of course but I think can be unnecessarily confusing, especially for learners.
@pontierent7343 жыл бұрын
@@CodingWithJohn I also saw that, but it definitely would be confusing to learners so it was probably better to put them on separate lines.
@paulsnehasish58302 жыл бұрын
im new to this channel and already loving the short gem contents
@moorefunandgames74112 жыл бұрын
Me too I just started an apprenticeship recently for Software Engineering and this channel has helped a ton on understanding some of the more complex features of the Java language.
@davidl67976 ай бұрын
Great explanation John! It was easy to learn and understand this algorithm with your lesson!
@tulioperez60113 жыл бұрын
Amazing teaching skills - perfection execution
@madhavansan0082 жыл бұрын
You are born to teach ! , Expecting more and more videos
@kishordige9721 Жыл бұрын
Best one, getting addicted to your videos!!!
@LONELY-x9k2 жыл бұрын
I know I am quite late to the party, but I really really liked your explanation and your code. A downside of using mergesort is because of the ammount of memory it requires. As you can see from your explanation, sorting an array of 1b ints means creating 1b arrays which can use a LOT of memory, now think of sorting 1b float, or other data-types which are much bigger. Mergesort is one of the quickest, easiest sorting algorithms but the obvious downside is because of how many resources it uses.
@fakhrulmb2 жыл бұрын
Subscribed! Helped me through my Algo and data structure unit!! Best coding tutorial out here!!
@rodrigosalomao38352 жыл бұрын
I've finilly learned this. Thanks a lot dude!
@AamirBilalm3 жыл бұрын
Great work John, really appreciate it. Highly undervalued channel. Hope you get the support you deserve.
@kunaldutta70962 жыл бұрын
Very good illustration and easy to understand steps. I have stumbled upon the videos of John by accident, and I am really happy that I have. Kudos to you. subscribed :) .
@wilsvenleong963 жыл бұрын
Definitely the best explanation out there. I'm subscribed.
@ferhatf93852 жыл бұрын
thanks alot i was reall struggling to learn merging sorted arrays ,but this video rwas eally helped me then all the others that i watched
@_Anna_Nass_2 жыл бұрын
Thank you so much! This was super helpful for my assignment. I feel like I really understand now.
@trentlandon90332 жыл бұрын
You really made it look so easy and understandable
@martinemanuel82392 жыл бұрын
It's just the best explanation I ever seen, thank you so much !
@asmaasadek77352 жыл бұрын
Just wanted to tell you, you are one of the best!
@georgikyshenko43802 жыл бұрын
That explanation was AMAZING ! Thank you !
@crazyedits69483 жыл бұрын
I am looking for this kind of explanation for a long time. Thank you so much john for this extraordinary tutorial. Loved it and subscribed immediatley.
@Svinqvai2 жыл бұрын
Great explanation. I would like a section at the end where you clean the code. Because you are lead dev you know you cannot have such code with 6 while loops and 3 for loops to sort an array. This code needs refactoring. That part would be beneficial. For example: using System.arraycopy or Arrays.copyOfRange to populate the left and right half of the array.
@okgus1 Жыл бұрын
Could he also have just passed the original array each time, just setting the starting/ending indexes of each sub array, so as to sort the "sub-arrays" in place, without having to allocate so much extra memory for each sub array?
@cesar-on-youtube Жыл бұрын
You're an excellent teacher. Thank you.
@michaellese36042 жыл бұрын
Great video, super easy to follow and made understanding merge sort way easier
@mariusandries41032 жыл бұрын
Thank you, this explanation is so clear and short.
@ayyanchira6 ай бұрын
Really good explanation! Thanks John! Your videos are spot on!
@MelonHusk72 жыл бұрын
Thank u john for putting it in my head! u got a lifetime subscriber.
@MrThemarsu3 жыл бұрын
soooo much better explanations than my lessons..... thx!
@juanjaureguireyes2 жыл бұрын
Hi John! Thanks a lot for your video. Quite helpful for my C++ programming assignment. Cheers!
@ВиталийКомлев-р8б2 жыл бұрын
Awesome explanation! As usual!!! Thank you, John, so much!!!! You're the best!!😊
@InvinciRD Жыл бұрын
I would be heavilyy grateful if make more vdos of such kind...y=you have the potential of conquering the market(by market i mean the hearts of students as your speech of explanation is crystal clear)
@thetruthsayer83473 жыл бұрын
I like this. I wish there more algorithm tutorials based in java. Please make more.
@ValenciaTheMan2 жыл бұрын
I guess even on a paid udemy course u still get a complicated explanation of mergesort with a difficult java code. This is clear and readable code, tahbk you very much, i could understand zhis clearly.
@willemvanderveen75672 жыл бұрын
Keep it up man, your videos are absolutely amazing
@nickynicole4030 Жыл бұрын
Thanks John! Very clear explanation
@fdrjanosch3 ай бұрын
Thank you very much for uploading this video. Greetings from Germany!