Learn Merge Sort in 13 minutes 🔪

preview_player
Показать описание
Merge sort algorithm tutorial example explained
#merge #sort #algorithm

// merge sort = recursively divide array in 2, sort, re-combine
// run-time complexity = O(n Log n)
// space complexity = O(n)

music credits 🎼:
===========================================================
Title: Wallflowers
Artist: Bad Snacks
===========================================================
Рекомендации по теме
Комментарии
Автор

This video took me THREE WEEKS to produce! Ya'll better smash that like button for me! lol

public class Main{

public static void main(String args[])
{
// merge sort = recursively divide array in 2, sort, re-combine
// run-time complexity = O(n Log n)
// space complexity = O(n)

int[] array = {8, 2, 5, 3, 4, 7, 6, 1};

mergeSort(array);

for(int i = 0; i < array.length; i++){
System.out.print(array[i]+ " ");
}
}

private static void mergeSort(int[] array) {

int length = array.length;
if (length <= 1) return; //base case

int middle = length / 2;
int[] leftArray = new int[middle];
int[] rightArray = new int[length - middle];

int i = 0; //left array
int j = 0; //right array

for(; i < length; i++) {
if(i < middle) {
leftArray[i] = array[i];
}
else {
rightArray[j] = array[i];
j++;
}
}
mergeSort(leftArray);
mergeSort(rightArray);
merge(leftArray, rightArray, array);
}

private static void merge(int[] leftArray, int[] rightArray, int[] array) {

int leftSize = array.length / 2;
int rightSize = array.length - leftSize;
int i = 0, l = 0, r = 0; //indices

//check the conditions for merging
while(l < leftSize && r < rightSize) {
if(leftArray[l] < rightArray[r]) {
array[i] = leftArray[l];
i++;
l++;
}
else {
array[i] = rightArray[r];
i++;
r++;
}
}
while(l < leftSize) {
array[i] = leftArray[l];
i++;
l++;
}
while(r < rightSize) {
array[i] = rightArray[r];
i++;
r++;
}
}
}

BroCodez
Автор

The way you broke this down into simple terms with visuals and everything, amazing. I'm not a bro, but I do code. And this was lit. Thank you.

mayawhocodes
Автор

You are single-handedly teaching me programming/computer science. Thank you. Never change your teaching structure, because this method works really well.

coltonbailey
Автор

This is the best video I found on all youtube which explains Merge Sort properly. This video is the definition of Quality Content.

jorgemedina
Автор

I knew when I saw "Bro Code" it would be a beautifully simple video.
What you did that the other videos I watched failed to do was walk through the sort / merge in the order that it would actually happen.
The other videos were doing it by rows and not the actual order that it happens in. Thank you ! That is what made it click for me.
I am now subscribed !

Jathamus
Автор

Bro, you are a legend. My professor went over merge sort for 1 1/2, and I was left confused. I watched a Harvard lecture video on it, still confused. Asked chatGPT about individual components (not to cheat but to have it explain each step to me, I want to understand this) and it was spewing nonsense. But your video made it finally click. Thanks bro! Liked and subscribed

GamerOverThere
Автор

Best explanation I've seen so far. The rundown at the beginning really helps. Thank you

kingipra
Автор

The best merge sort explanation on YouTube. Watched like 5 other videos but only this explained how the merge part of merge sort actually works! Liked and subscribed.

smokinginthefishroom
Автор

So clear. my professor did not have enough time to go over this in class. Now I understand everything about merge sort.

raywei
Автор

Idk you sounds more confident in recent new videos, keep going homie you still gotta reach more people👍

sanskarsongara
Автор

You are a great tutor sir, I appreciate, I was failing to understand this all along, but just because of your video, I feel confident enough

nyasha_keith_matevengwe
Автор

First of all I just wanted to let you know that thanks to your java section I landed a job as a java developer.
So ...from the bottom of my heart, thank you, keep up the good work!

On the other hand, a question for this video: what if the array has an odd length ?

valioprea
Автор

I'm on a journey of learning DSA ... I've got to say, you explain things very well! Glad I've stumbled on your channel.

Nootey
Автор

You made mergeSort look so simple and easy, thank you so much!

nyankiku
Автор

Great video. I needed to implement this in C for a CS class and this video explained it in much clearer and easier to follow way than the so called experts who are being paid 6 figures a year to teach this to me. Ty bro.

NH-ijdz
Автор

this helps me understand how the algorithm works, thank you bro for this detailed explanation. ^_^

daviduntalan
Автор

Thank you for the beautifully elegant explanation that didn't require the viewer to have a PhD. You are one talented bro. Keep it up!

aadvk
Автор

After 2 years this is still very useful i understand everything now .
even though i don't know how to use java i've just learned c++ but everything is clear now

zyad
Автор

Probably the best video i've seen so far to explain Mergesort, i think i finally got it!

danielebbersmeyer
Автор

Thank you man!!! Finally I have found the best explanation of merge sort! You really helped us a lot.
I've alredy smashed the like button, you deserve it bro!!!

paulodasilvasa