How to Code The Merge Sort Algorithm in Java

  Рет қаралды 85,697

John Kwisses

John Kwisses

Күн бұрын

Пікірлер: 105
@aishikbhattacharya4296
@aishikbhattacharya4296 6 жыл бұрын
Best step by step explanation of merge sort code in KZbin
@divyamehta2863
@divyamehta2863 5 жыл бұрын
initialization for right array can be resolved by taking ,int right[] = new int[array.length-mid]; Otherwise really a good explanation of code.
@neolegionar
@neolegionar 4 жыл бұрын
True but first thing that came to my mind was int right[] = new int[mid+array.length%2]
@phabove7
@phabove7 3 жыл бұрын
@@neolegionar : It should be division and not modulo, right? Mod 2 will only return either 0 or 1.
@DR-ng7oh
@DR-ng7oh 2 жыл бұрын
I cannot even begin to say how much this helped
@greentorainchannel386
@greentorainchannel386 2 жыл бұрын
One of the best video in youtube for merge sort.. 👍👍👍👍👍 easy to understand and remember...
@iamarnabpal
@iamarnabpal 3 жыл бұрын
This is the beauty of bottom-up approach. This tutorial is so easy to understand because Johnathon did a bottom-up approach. Hands down, the best tutorial on merge sort in Java on YT. This video should have had 1 million likes.
@deepakmodi10
@deepakmodi10 3 жыл бұрын
No words to describe how easy you made this algo. Easy to understand.
@markd2797
@markd2797 3 жыл бұрын
2:46 this is ONE EXAMPLE of what made your tutorial different and exponentially better than other tutorials. You stopped and asked “what if” then proceeded to answer that “what if” immediately. Other tutorials just starts with an array of size 8, explains merge sort as “DiViDe aNd cOnqUeR” then call it a day. So, I’d like to say thank you for making this great tutorial.
@rachamallidorasrivignesh4499
@rachamallidorasrivignesh4499 2 жыл бұрын
The way of teaching is excellent johnathon tq.
@xingjianwang1894
@xingjianwang1894 4 жыл бұрын
This explanation is simpler and better than GeeksforGeeks, tbh.
@saikiranj807
@saikiranj807 5 жыл бұрын
Although I don't take paid courses online , I seriously think you should make one. You have a natural talent at explaining code. Seriously, it's the best explanation on KZbin, ever!
@iyadshaheen4693
@iyadshaheen4693 3 жыл бұрын
just came across this, amazing video, I do have one comment and it just simplifies the code much more. my comment is that if the array of size 7 -> mid point is 7/2 = 3 you initialize that first array with size 3 and then you initialize the right array with, (consider array is the main original array name), array.length - midpoint size and that is 4 so that way you don't really need all those if else statements. it works if the array was of size 8 too (even split). other than that greatttt work and very good vid
@javaexpertsa8947
@javaexpertsa8947 5 жыл бұрын
Unbelievable Tutorial, i tried so many courses and watched so many videos and still couldn't understand Merge Sort, because the instructor didn't explain the code well or just show the code without saying a single word. Thank you so much!
@yolopassion
@yolopassion 6 жыл бұрын
The best explanation of Java Code for Merge Sort on You tube
@nguyenphuc2046
@nguyenphuc2046 4 жыл бұрын
best explanation you could found on youtube
@bm1078
@bm1078 3 жыл бұрын
Thankyou so much, this explanation is literally so much better than what was in my textbook lol
@cenkercanbulut3069
@cenkercanbulut3069 5 жыл бұрын
Finally, someone, with full control over what he does , explaining it... A BIG THANKS LIKE AND SUB!
@mongoose1618
@mongoose1618 4 жыл бұрын
This is so much simpler to understand and easy to remember, Thank you! However, I think you must correct your IF condition at line 71 to if(left[leftPointer]
@twitjunkie6007
@twitjunkie6007 3 жыл бұрын
Are you talking about the case where elements in the left and right are same ? I tried creating an array with same elements and ran this code. It is working.
@phabove7
@phabove7 3 жыл бұрын
I think his code will also produce the same result. Because, without equality check, the first element in the right half should be the same.
@HorizonSpeed26
@HorizonSpeed26 4 жыл бұрын
Thanks for the code explaination, now the implementation of merge sort makes sense in terms of the conceptual explaination of merge sort.
@blastoice9281
@blastoice9281 3 жыл бұрын
Gotta say, I like your solution the best. Clear explanation too, thanks alot.
@DeepsWar
@DeepsWar 4 жыл бұрын
This is such a great explanation. Had been struggling to understand the concept and code accordingly and this implementation & explanation is too good. Thanks again!
@pradyumna668
@pradyumna668 4 жыл бұрын
your explanation helped me a lot keep doing more videos on ds and algo
@minhtanchu240
@minhtanchu240 3 жыл бұрын
Thank you so much! Easy to understand.
@filiptomic8531
@filiptomic8531 5 жыл бұрын
Omg thank you so much this is the best explanation I found on KZbin
@mikaelalvian8160
@mikaelalvian8160 3 жыл бұрын
Amazing explanation! Thank you, Sir!
@laleen123
@laleen123 4 жыл бұрын
By far the best version, Thank you
@Morkizas
@Morkizas 5 жыл бұрын
The f***g BEST explanation of Merge Sort, you saved me a lot of nerves :)
@romannejouta6546
@romannejouta6546 3 жыл бұрын
Very well explained!!! Thank you!
@liyingfeng2464
@liyingfeng2464 3 жыл бұрын
super clearly explained, thank you so much!
@ramansb1008
@ramansb1008 3 жыл бұрын
Excellent explaination!
@yahiamostafa4718
@yahiamostafa4718 5 жыл бұрын
The Best Explanation
@aneilievska9883
@aneilievska9883 4 жыл бұрын
OMG this was awesome, please when you have time cover Quick Sort.
@rajshreerai628
@rajshreerai628 3 жыл бұрын
thank you.. this was awesome
@vinayr2265
@vinayr2265 5 жыл бұрын
wow.. excellent explanation.. the best one i came across.. thank you very much
@hegyilevente221
@hegyilevente221 4 жыл бұрын
That one was life saving. Thank you!
@davidaraujo2049
@davidaraujo2049 5 жыл бұрын
Thank you soo soo much, you just saved my exam tomorrow !!!
@maddy3656
@maddy3656 3 жыл бұрын
Very helpful, thank you!
@rejithregi2588
@rejithregi2588 5 жыл бұрын
very clearly explained
@Superb_virg
@Superb_virg 5 жыл бұрын
Thank you great explanation. I have one question however: why does the incrementing of the pointers have to happen when they are used and not after? 18:05 And why do you get a weird output if you do not?
@ankitjoshi6545
@ankitjoshi6545 5 жыл бұрын
Hey Samuel, It is not necessary to do leftpointer++. He just meant that do not forget to increment. you can split that (single line) result[resultpointer++] = left[leftpointer++]; is same as {three lines of code} result[resultpointer] = left[leftpointer]; resultpointer++; leftpointer++; you will get weird output if you do not increment . you will remain at the same pointer as previous iteration. you need to increment pointer to point next index in next iteration right.
@rixflores4204
@rixflores4204 5 жыл бұрын
Bro wtf you are the best 👌 thanks from mexico
@mohdarshad9427
@mohdarshad9427 4 жыл бұрын
best step by step code explaination ever found on youtube for merge sort thank u so much . how can i contact with u ?
@palaksdarji
@palaksdarji 4 жыл бұрын
This is superb.
@Joy-mf6xo
@Joy-mf6xo 3 жыл бұрын
Thank you!
@Mona001-01g
@Mona001-01g 4 жыл бұрын
Johnathon could you please also make a video for binary search in java. You explain so well. Somehow I understand much better when you do since i tried other videos in vain
@ragabhassan1043
@ragabhassan1043 3 жыл бұрын
that was great thank you
@willemvheerden7185
@willemvheerden7185 5 жыл бұрын
Awesome tutorial
@zeeshanhasnani
@zeeshanhasnani 6 жыл бұрын
Thanks a lot! Your explanations were awesome and concise! :D
@znveleisi1566
@znveleisi1566 3 жыл бұрын
what should i change to make it sort in a descending order
@sivaprasath8766
@sivaprasath8766 3 жыл бұрын
Awesome!!!!
@SinteXCod
@SinteXCod 5 жыл бұрын
Ty mate, now I finally understand it!
@bouhenniouahiba8892
@bouhenniouahiba8892 4 жыл бұрын
i need help , how does the synchronization between several users (modify the same shared file) with algorithms
@charlibravo2578
@charlibravo2578 5 жыл бұрын
Really helpful! Thank you so much!!
@eoghanfeighery7383
@eoghanfeighery7383 3 жыл бұрын
Just out of curiosity, is it possible to this program with an instantiable class containing multiple datatypes (String, integer, double)
@luckypatilbhosle007
@luckypatilbhosle007 5 жыл бұрын
I understood code .........Thank you sir...........
@goodyramsesgarciatrujillan4035
@goodyramsesgarciatrujillan4035 3 жыл бұрын
ur the best ty
@sdfghuihef5602
@sdfghuihef5602 3 жыл бұрын
Thank you
@606shann
@606shann 4 жыл бұрын
Awesome sir, Thanks a lot
@FutureWiredTec
@FutureWiredTec 5 жыл бұрын
Thank you very much that was very helpful
@loisesp8061
@loisesp8061 3 жыл бұрын
Where did you compile this? What app did you use to run this code. Thanks in advance :)
@ramgopal2520
@ramgopal2520 5 жыл бұрын
I am somewhere stuck in the recursion of the method Merge. Could someone help me with it. Other than that I understood everything. And I have been going through merge sort for a lot of days and this guy nailed it with his explanation !! Still stuck in the recursive part 😢
@kshitijnegi8151
@kshitijnegi8151 2 жыл бұрын
I am stuck too
@Mona001-01g
@Mona001-01g 4 жыл бұрын
What happens when both leftpointer and righpointer values are equal?
@muhammadahmadi7096
@muhammadahmadi7096 4 жыл бұрын
Wow, was amazing 😍👌
@irfancalsr7982
@irfancalsr7982 3 жыл бұрын
Amazing
@UKcouple173
@UKcouple173 6 жыл бұрын
clearly explained thank you so much
@adithyanalluri4881
@adithyanalluri4881 5 жыл бұрын
Excellent!!
@vincemarkarnedo6644
@vincemarkarnedo6644 5 жыл бұрын
Great vid. But please next time add comments
@tufanatha7798
@tufanatha7798 4 жыл бұрын
Awesome explanation ! few lines of coding can be reduced (27~35) by replacing: int[] right=new int[array.length-middle];
@pointless8846
@pointless8846 5 жыл бұрын
Hey I really liek your video, I am pretty new to Java and I had a question about incrementing in your merge method, specifically about left[leftPointer++], wouldn't it increment first then chose the index. For example: leftPointer = 0 left[leftPointer++] would become left[1] or does in take leftPointer as the index then it increments? I am not sure if you understand my question.
@-0----0----0----.-----.----.-7
@-0----0----0----.-----.----.-7 4 жыл бұрын
no it willl not....its postfix increment right...soo it willl only increment after....prefix which is ++x will do exactly as you said...but not postfix x++
@pradyumna668
@pradyumna668 4 жыл бұрын
thank you so much
@drikast
@drikast 4 жыл бұрын
why did you increment: "result[rightPointer++] = right[rightPointer++]"? isn't it supposed to start from the first element [0]? Won't the increment point at the second[1]?
@-0----0----0----.-----.----.-7
@-0----0----0----.-----.----.-7 4 жыл бұрын
no it willl not....its postfix increment right...soo it willl only increment after....prefix which is ++x will do exactly as you said...but not postfix x++
@luckypatilbhosle007
@luckypatilbhosle007 5 жыл бұрын
It is not working for large arrray.............. ArrayIndexOutOfBoundException is coming
@waynemack6637
@waynemack6637 5 жыл бұрын
does this with an odd amount of items in array
@eagerassaultixopi4558
@eagerassaultixopi4558 5 жыл бұрын
at 2:08 i don't understand the usage if(array.length
@GFC1337
@GFC1337 5 жыл бұрын
because if the array is of size 1, then it's [8] for example. Therefore it's already sorted. It's impossible for 1 number to be sorted.
@qaipak1
@qaipak1 6 жыл бұрын
Correct me if wrong (and this might be nitpicking if I'm not) but around 12:00 when you are checking which one is less, should it not be less than equals to? Otherwise in equal cases the right and left will swap. Yes your array will be sorted but I believe Merge sort is supposed to be in place i.e, inputs should be in the order they were inserted in. Again I might be wrong being a novice.
@JohnKwisses
@JohnKwisses 5 жыл бұрын
I believe that you are correct. The values will be the same, but for best practice, you should use
@qaipak1
@qaipak1 5 жыл бұрын
@@JohnKwisses Thanks. Great video btw!
@MYmotivationn
@MYmotivationn 4 жыл бұрын
perfect bro
@jacobjacob5735
@jacobjacob5735 6 жыл бұрын
Is this faster than comparing two numbers and switch their places within a counter for i and j?
@arnoc.2107
@arnoc.2107 6 жыл бұрын
It is way more efficient. The runtime of a well-implemented mergeSort-algorithm is log(n) in the worst case. Meanwhile, having two for-loops comparing two elements at a time and switching them if necessary, has a WAY longer runtime.
@manis8483
@manis8483 5 жыл бұрын
Hi Johnathan Your way of explaining the code was just amazing. But when i try to recreate your code, i am getting an array out of bound exception. my code.... package sortingAlgo; public class MergeSorting { private static void printArray(int[] ar) { for (int i : ar) { System.out.print(i + " "); } System.out.println(); } private static int[] mergeSort(int[] ar) { if (ar.length
@JohnKwisses
@JohnKwisses 5 жыл бұрын
Change this: for (int j = 0; j < right.length; j++) { right[j] = ar[midpoint + 1]; } To this: for (int j = 0; j < right.length; j++) { right[j] = ar[midpoint + j]; } In your mergeSort method (changing the 1 to a j).
@vishalbisht5554
@vishalbisht5554 5 жыл бұрын
The code explained by tutorial has error it will never work for larger array.
@jm784952
@jm784952 4 жыл бұрын
okkkkk. and i thought it was my mistake....how can i fix this?
@curtistackie7459
@curtistackie7459 6 жыл бұрын
I don't understand what you mean by "while there are elements left in the array" at 17:37 when you don't delete any elements from any arrays at any point.
@JohnKwisses
@JohnKwisses 6 жыл бұрын
The while loop will only run if there are elements still in the left or the right array starting from the left or right pointer position onwards. If a pointers position is equal to the length of its respective array, it means that there are no more elements to check in the array and the loop does not need to be run. For example, the loop would not run if the arrays did not contain any elements (the length of the array is not less than the position of its pointer). Another example, the loop would run if the left array had one element and the right array was empty (the length of the left array would be less than the position of its pointer). Same as if the right array had one element and the left array was empty (due to the exclusive OR operator as defined in the while loop). Hope this helps!
@3bdoelnaggar
@3bdoelnaggar 5 жыл бұрын
THANKS
@bantikumar8836
@bantikumar8836 6 жыл бұрын
too good !!!!! but these are easily understandable need clarity in graph and trees
@samirallahverdi4948
@samirallahverdi4948 4 жыл бұрын
What a long soluthion!
@AryanSingh2512.
@AryanSingh2512. 5 жыл бұрын
Why java code is so much different than C?
@castro6612
@castro6612 5 жыл бұрын
because they are different language
@-0----0----0----.-----.----.-7
@-0----0----0----.-----.----.-7 4 жыл бұрын
me as a programmer duh.. 5:09
@shadmansudipto7287
@shadmansudipto7287 3 жыл бұрын
at 8:30 you create pointers, but you do not explain what you mean by pointers, wastes a lot of time trying to understand that.
@minhbui4817
@minhbui4817 5 жыл бұрын
I love you. Plz have my baby
Quicksort Sort Algorithm in Java - Full Tutorial With Source
24:58
Coding with John
Рет қаралды 249 М.
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 43 МЛН
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 31 МЛН
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 317 МЛН
Merge Sort step by step walkthrough (Recursion)
7:29
Stephen O'Neill
Рет қаралды 98 М.
Quick Sort Using Recursion (Theory + Complexity + Code)
42:14
Kunal Kushwaha
Рет қаралды 186 М.
Merge Sort Algorithm in Java - Full Tutorial with Source
23:02
Coding with John
Рет қаралды 185 М.
Java Program #23 - Sort Numbers using Selection Sort in Java
8:24
Programming For Beginners
Рет қаралды 25 М.
Learn Merge Sort in 13 minutes 🔪
13:45
Bro Code
Рет қаралды 332 М.
Merge Sort Java Algorithm Code Example
31:13
Java Code Geeks
Рет қаралды 9 М.
Merge Sort
12:48
Lalitha Natraj
Рет қаралды 57 М.
Learn Quick Sort in 13 minutes ⚡
13:49
Bro Code
Рет қаралды 377 М.
How to: Work at Google - Example Coding/Engineering Interview
24:02
Life at Google
Рет қаралды 7 МЛН
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 43 МЛН