Merge sort in 3 minutes

  Рет қаралды 1,261,287

Michael Sambol

Michael Sambol

Күн бұрын

Пікірлер: 330
@CamFocusApp
@CamFocusApp Жыл бұрын
For people who didn't understand the merge part like I didn't when I watched this here's an explanation. when we finalize the divifing into smaller arrays part, we will have arrays from which we always know the smallest item, which is the left most. so we compare the left most item from each array and the smaller one we put it first into the bigger array. and we do that until we complete the bigger array.
@MichaelSambol
@MichaelSambol Жыл бұрын
👍🏼👍🏼💪🏼
@loveabhinav
@loveabhinav Жыл бұрын
Thankyou very much!! Most of the tutorials lack this explaination.
@黄嘉宇-z6l
@黄嘉宇-z6l Жыл бұрын
ty
@unitedrail-mainchannel8991
@unitedrail-mainchannel8991 Жыл бұрын
Thanks mate, makes sense now (EOY exams GCSE in 3 days LOL)
@paul_tee
@paul_tee Жыл бұрын
there's no way this is correct - if you do your "merge" algorithm on [1,2] and [3,4] you end up with [1,3,2,4]. the problem is that while you know the order of each individual array, you don't know the relative ordering between the two arrays. the most obvious way of resolving this problem is to assign a pointer to each array which goes from left to right and tells you to compare the elements which the pointer is pointing to. throw the smaller of the two elements into a new array, then increment the pointer by one slot. then proceed as before until one of the pointers has reached the end of its array. then throw the remaining stuff from the other array into the new array.
@kanrup5199
@kanrup5199 5 жыл бұрын
The merging part itself needs to be more explained, it seems a bit mysterious to me going from two element pairs to a set of four elements.
@Ayoubsss
@Ayoubsss 5 жыл бұрын
Yes that's what I thought
@sorenbellamy3382
@sorenbellamy3382 5 жыл бұрын
dont u just use selection sort to do that bit?
@yahavx
@yahavx 5 жыл бұрын
@@sorenbellamy3382 You can but you are not using the assumptions that both arrays are sorted by themselves and hence missing the whole point. Doing at will cause the algorithm the operate at at least n^2. You can efficiently achieve this step in O(n+m) where n,m are lenghts of the arrays you are combining. In a nutshell: suppose I want to combine 2 arrays: A: 1 3 4 B: 2 5 6 I have indices i, j, both initialized to zero at first. now i compare A[i] to B[j]. Because A[i] is smaller, I add A[i] to my new list, and increment i by one. My new list is now: [1], and i = 1, j = 0. Now I compare A[i] to B[j] again. Now B[j] is smaller, so I add B[j] to my new list, and incremnet j by one. My new list is now: [1 2], and i = 1, j = 1. Again: A[i] < B[j] so add A[i], now list is [1,2,3], i = 2, j =1, and so on. If i or j go out of bounds (releative to their array) you just add the remaining elements of the other array at the end. P.S. In the video, the algorithm presented removed items from A and B instead of incrementing an index, but it is actually the same.
@vugarasadov4016
@vugarasadov4016 5 жыл бұрын
Just merge 2 sorted array
@rapidreaders7741
@rapidreaders7741 5 жыл бұрын
You create a new Array (let's call it "Merged") that's the same size as the two unmerged arrays combined. Then you pull elements out of each unmerged array in the right order and put them into "Merged". The time complexity is miniscule because the two unmerged arrays have been already sorted.
@aniruddhkarekar2818
@aniruddhkarekar2818 Жыл бұрын
Your videos are best for those who have already done this thing and it has been long time since revised.
@MichaelSambol
@MichaelSambol Жыл бұрын
thanks man!
@3x6249
@3x6249 4 жыл бұрын
Half hour til exam and I am here.
@tylerfish939
@tylerfish939 3 жыл бұрын
How’d the exam go?
@3x6249
@3x6249 3 жыл бұрын
@@tylerfish939 stiill made some mistakes lol
@yutaitadori7318
@yutaitadori7318 3 жыл бұрын
@@3x6249 wow
@Dark_Peace
@Dark_Peace 3 жыл бұрын
4 days 'til exams and I'm here
@raphaelramos4016
@raphaelramos4016 3 жыл бұрын
@@Dark_Peace How’d the exam go?
@misskyleigh2999
@misskyleigh2999 12 күн бұрын
Thank you very much for this helpful explanation and visualization. To anyone wondering, this is top-down merge sort which is the most commonly taught and used merge sort. Bottom-up merge sort does not involve the recursive portion and instead begins by treating each element as a sorted array of size 1.
@VinodMoorkoth
@VinodMoorkoth 3 жыл бұрын
"Inserting items in correct order." - That's the whole algorithm isn't it?
@WingedGlider
@WingedGlider 3 жыл бұрын
Thank you for being so simple and concise with your explanations. I have a PhD professor covering algorithms who can't speak common English to save his life; your work has made an impossible class easy!
@drditup
@drditup 11 ай бұрын
this is amazing. no fluff, no babbling. just a direct explanation. im so used to people talking for an extra 20 minutes when this is all it takes. great, just great
@koshka02
@koshka02 Жыл бұрын
Man, you summarize hours of lectures into clean, concise, easy to follow steps. Thank you.
@AbinashAdhikari
@AbinashAdhikari 4 жыл бұрын
What is the point when you leave the most important part which is logic of how the sorted subarrays are merged such that the result is sorted as well.
@Nakameguro97
@Nakameguro97 4 жыл бұрын
The merge part is trivial to understand, even though the pseudocode appears much longer than the recursive part. Suppose you start with two arrays/stacks that are sorted within itself. Compare the top of the two stacks. Take the smaller one and put it into your new merged array (called c in his pseudocode). Repeat until you drain one of the stacks. Move the rest of the other, non-empty stack into your merged array.
@DarkKnightofIT
@DarkKnightofIT 3 жыл бұрын
@@Nakameguro97 it may be "trivial" for you to understand, but for other people it may be difficult to understand. Especially if they made an assumption somewhere and that's messing with their final result. Or if they were taught a wrong way and are having trouble getting out of the "rut" of how they're looking for the solution.
@Nakameguro97
@Nakameguro97 3 жыл бұрын
@@DarkKnightofIT It's trivial compared to the recursive part - I was trying to explain why the video doesn't talk about it. Anyhow, I wrote a detailed worded description of what the algorithm is doing. If that's not enough, someone else will have to help explain.
@rcgldr
@rcgldr 7 жыл бұрын
Recursive merge sort is popular in classroom environments, but most actual libraries implement a variation of bottom up merge sort, which is iterative, not recursive. Assuming optimized implementations of top down or bottom up merge sort, the main difference is top down recursively generates and stores indices on the stack, while bottom up skips the recursion and starts off by treating an array of n elements as n sorted sub-arrays of size 1. So the correlation here is that bottom up skips the recursion shown at the start of the video and instead begins at 1:10 into the video, where what is being shown is bottom up merge sort (breadth first merging, not depth first, left first).
@everlastinGamer
@everlastinGamer 3 жыл бұрын
Pay no attention to that^^ they’re just upset that they don’t understand. Your comment was very useful.
@arasefe
@arasefe 3 жыл бұрын
Looking at the algorithm, that's what I exactly could not undestand, why would you need first to separate an array to 1 element arrays. Your comment really helped, thank you.
@tsutsen1412
@tsutsen1412 2 жыл бұрын
Damn... Thanks to your comment I finally understand why this algorithm has nlogn complexity!
@ibrahimfaiz501
@ibrahimfaiz501 2 жыл бұрын
Your videos sum up my classes in just a couple of minutes. Amazing work!! Life saver!
@MichaelSambol
@MichaelSambol 2 жыл бұрын
Thanks, Ibrahim!
@brycehoch2963
@brycehoch2963 5 жыл бұрын
Thank you so much for these videos. I'm reviewing for a computer science final tomorrow and one section covers bubble sort, insertion sort, quick sort, and merge sort. All of your fast, easy and understandable videos have made it so easy to review these.
@brycehoch2963
@brycehoch2963 2 жыл бұрын
@@Anonymous_United_Official thank you for haunting me three years later when I am now out of college and have the absolute most burning hatred for computer science
@redhawkneofeatherman261
@redhawkneofeatherman261 2 жыл бұрын
That was some sad plot development
@Bala-oj1bm
@Bala-oj1bm Жыл бұрын
@@brycehoch2963 how did u feel about cs in college? consider me in ur shoes 4 years ago
@capybara8134
@capybara8134 Жыл бұрын
Thank you so much. Looked at my uni lecture slides and dint understand a single bit of it. Watched your vids and totally understood it. Thank you very much❤
@MichaelSambol
@MichaelSambol Жыл бұрын
You're welcome! Thanks for watching.
@doc9448
@doc9448 6 ай бұрын
This helped me solidify my understanding after sitting through a lesson that covered Mergy Sort.
@dbella804
@dbella804 6 жыл бұрын
Thank you so much, for simplifying these concepts. It was very difficult for me until I watched your videos.
@stephenweber33
@stephenweber33 2 жыл бұрын
Give you a perfect score for the right tone and tempo in your voice. Everything is here.
@Pirateboi-vt5rp
@Pirateboi-vt5rp 7 ай бұрын
wow this is so much easier than the example on Khan Academy. Much easier to see how the actual code works through your writing.
@brendabrownofficial
@brendabrownofficial 6 жыл бұрын
You are such a great teacher, universities need more professors like you
@richardtwitty6126
@richardtwitty6126 7 жыл бұрын
Wow, you did a better job than any other youtube video with 3 min. Thanks!
@PixelBlock1k
@PixelBlock1k 5 ай бұрын
Appreciate it you made it simple, i couldnt understand it in my class cause i hate my seating plan
@arielsanchez9775
@arielsanchez9775 3 жыл бұрын
l love the way you explain Merge Sort because it is easy and intuitive
@GoeunseoGo
@GoeunseoGo Күн бұрын
2:20 이 부분을 보다가 의문이 들었습니다. 2358과 1479을 합쳐서 다시 배열하는 게 된다면 맨 처음 배열에서 반으로 나누고 바로 정렬을 하면 되는 거 아닌가요? 왜 꼭 배열을 더 이상 나눌 수 없을 때까지 계속해서 반으로 나누는 건가요? *짧고 함축적인 영상 감사합니다. Sort Algos재생목록이 특히 좋아요! While watching the part at 2:20, I had a question. If you can combine 2358 and 1479 and then rearrange them, wouldn't it make sense to just split the initial array in half and sort it right away? Why do you need to keep dividing the array until you can't divide it any further? *Thanks for the concise and well-made video! I especially enjoy the Sort Algos playlist!
@madfury2827
@madfury2827 8 ай бұрын
damnn man, my teacher took an hour on this and you just explained it crystal clear in four minutes! hats off
@jon-sw5yw
@jon-sw5yw 4 жыл бұрын
This is great for basic understanding but for the transition to understanding the code it's best to do it step by step with some code
@CC-bm3wb
@CC-bm3wb 3 жыл бұрын
I absolutely agree. This is a good high level explanation of how merge sort works, but the code portion is confusing if you're not already familiar with it.
@matts8791
@matts8791 Жыл бұрын
Awesome video, way better than my professor!
@dawiz9671
@dawiz9671 6 жыл бұрын
Great, I learned more in 3 minutes of my life than I have within a week of APCS
@tianhaowang5012
@tianhaowang5012 6 жыл бұрын
clean screen, detailed explanation, thanks for the work you have done!!
@mrkekson
@mrkekson 3 жыл бұрын
"Inserting items in the correct order", thx man :D
@6Pope9
@6Pope9 3 жыл бұрын
Yeah good luck with saying that at the exam lol
@thecatoftime8152
@thecatoftime8152 3 жыл бұрын
god thank you, this is way simpler than the other stuff
@rudi8192
@rudi8192 Жыл бұрын
The algorithm is one thing. Implementing it c or other programming language is the real deal. When i first implemented it in university you call a function tha passes an array, the first index of the array 0 called min and the last index n-1 max. As long as min
@MichaelSambol
@MichaelSambol Жыл бұрын
Code sample here: github.com/msambol/youtube/blob/master/sort/merge_sort.py :)
@R3ynco
@R3ynco 6 жыл бұрын
The explanation is great but it would be awesome to see an arrow pointing at each number when deciding which number is smaller. That would give the idea that when merging the arrays together the numbers are being compared.
@KY-xz9yb
@KY-xz9yb 2 жыл бұрын
2:11 no idea how the program is comparing and sorting the pair of four elements.
@zichenglim9777
@zichenglim9777 4 жыл бұрын
Nice video, short enough and easy to understand.
@salthesadmanshark5645
@salthesadmanshark5645 3 жыл бұрын
I read the wiki article and saw bunch of other yt videos but couldn't write this myself , but this video helped me finally write a top_down mergesort algo in ruby. So thanks a lot for the video.
@LucidMlem
@LucidMlem 5 жыл бұрын
6: Am I a joke to you?
@andrewcheng1948
@andrewcheng1948 4 жыл бұрын
You stole my idea!😀
@rimberse3405
@rimberse3405 4 жыл бұрын
lmao xD
@leomonz
@leomonz 4 жыл бұрын
how can you tell there is no 6.... could be impostor
@LucidMlem
@LucidMlem 4 жыл бұрын
Haha there are 66 likes 6 got what it deserved
@IStMl
@IStMl 4 жыл бұрын
@@leomonz 6 sus
@m.svinayak7277
@m.svinayak7277 3 жыл бұрын
My man, you have to do more vdos like this i love your vdos
@forprogramming8541
@forprogramming8541 Жыл бұрын
Thanks for your videos sir. I got an "A" for data structures and algorithms module.
@MichaelSambol
@MichaelSambol Жыл бұрын
Let's go! 💪🏼❤️
@adognamedcarlo
@adognamedcarlo 4 жыл бұрын
Many thanks Michael. Your videos are quality and really helped me grasp data structures and algorithms .
@TheMeticulousMoo
@TheMeticulousMoo 3 жыл бұрын
awesome video dude. I particularly liked the pseudocode. Thanks a bunch
@vekeron
@vekeron 2 жыл бұрын
thanks for the consise nature of this. helped refreshing before a quiz
@srinivasnangunuri1313
@srinivasnangunuri1313 5 жыл бұрын
simplest explaination on Merge sort. Bravo!
@hibahhussain2944
@hibahhussain2944 5 жыл бұрын
Great, at least I can now not be scared if this comes up on my GCSE
@tourajvaziri9840
@tourajvaziri9840 Жыл бұрын
Thanks very much again Michael. You are awesome!
@ob2344
@ob2344 11 ай бұрын
great vid! simple and easy to understand for a review
@hibakhan9764
@hibakhan9764 Жыл бұрын
this is exactly what i was looking for
@helloworld-rv3zw
@helloworld-rv3zw 6 ай бұрын
This really helps. Thanks
@MichaelSambol
@MichaelSambol 6 ай бұрын
💪🏼❤️
@daikikaminari6360
@daikikaminari6360 5 жыл бұрын
Thank you so much ! I was stuck for hours and this video helped me understand how it was actually working and then it was easy to implement :)
@avtaras
@avtaras Жыл бұрын
Please do a video on Knuth-Morris-Pratt algorithm! 🙏🏼
@cristianmendoza9440
@cristianmendoza9440 Жыл бұрын
new sub! great videos! thanks ! we need another video with RadixSort please!
@MichaelSambol
@MichaelSambol Жыл бұрын
Thanks man!
@willmartin1748
@willmartin1748 Жыл бұрын
Please do a video on how the actual recursive stack works with this algorithm. That's the confusing part for me.
@leelap9033
@leelap9033 4 жыл бұрын
This is the video Iam searching for.......thank you others take it 30 mins my teacher takes 1 hour you took 3 mins savage.....thug......thank you.......😀
@mystictech629
@mystictech629 2 жыл бұрын
Set playback speed to 2x and learn in 1.5 min
@aakashps7079
@aakashps7079 6 ай бұрын
Hfasdre
@ADevStory
@ADevStory 4 жыл бұрын
Really good and really short! Awesome video!
@this_is_mac
@this_is_mac 2 жыл бұрын
Dang it. You could've saved more time by speeding up the animation. Great content btw!
@akshaysethia6879
@akshaysethia6879 6 жыл бұрын
this is the best video I have seen for merge sort
@avrcharacter6750
@avrcharacter6750 11 ай бұрын
Genius way learn in less time 👍😊
@berna031
@berna031 2 жыл бұрын
suggestion, you could add why you need an extra function in this implementation ( i don't know why but everybody uses it that way)
@codewithnacho
@codewithnacho 5 жыл бұрын
This is a perfect explanation of Merge sort! Thanks for sharing this.
@JoseSanchez-ye3dn
@JoseSanchez-ye3dn Жыл бұрын
Why do I pay for University when I got this man wtf
@cretudavid8622
@cretudavid8622 2 жыл бұрын
So you divide, than you merge?
@luciorodriguez6616
@luciorodriguez6616 6 жыл бұрын
this video was the best I've found so far, thanks!
@K_OAT
@K_OAT 2 жыл бұрын
Thank you! love your simple and informative explanation!
@tylersnard
@tylersnard 3 жыл бұрын
Why wouldn't you just merge it all into one array from the beginning of step 2, rather than merge into individual arrays first?
@dorgn
@dorgn 5 жыл бұрын
A great explanation up to the point, Thank you so much!!
@AbdurRahman-mv4vz
@AbdurRahman-mv4vz Жыл бұрын
Thank you for the helpful video!
@MichaelSambol
@MichaelSambol Жыл бұрын
you're welcome!
@vskrand
@vskrand 5 жыл бұрын
Thank You very much. Helped out with my AP CS lab
@sallaklamhayyen9876
@sallaklamhayyen9876 7 ай бұрын
good explanation = thank you so much❤
@elshroomness
@elshroomness Жыл бұрын
You should make another video explaining the merge sort psuedo code as well. The diagram helped me understand but the code at the end seemed a bit hand wavy for to me to understand.
@MichaelSambol
@MichaelSambol Жыл бұрын
For sure, good call out. In my earlier videos I wasn't as focused on the pseudocode, which I regret. Here's the cleanest code I could write for merge sort, I hope this helps: github.com/msambol/youtube/blob/master/sort/merge_sort.py. I may revisit some of these over time.
@barisbasar3909
@barisbasar3909 2 жыл бұрын
great vid, actually helped me understand it very well , thank you
@hengzhou4566
@hengzhou4566 3 жыл бұрын
三分钟看懂不难,关键是能三年记住。
@reportimojega1522
@reportimojega1522 3 жыл бұрын
dui
@brokenvectors
@brokenvectors 2 ай бұрын
I think this video would be more complete if there was an example for arrays of odd length.
@chrisogonas
@chrisogonas 8 ай бұрын
Simple and clear.
@samimaameri9925
@samimaameri9925 6 жыл бұрын
Best video ive seen on merge sort so far. Pseudo code is great
@adimihir
@adimihir Жыл бұрын
So in this you basically have to divide and conquer and then arrange the numbers in ascending order right?
@TrevorHigbee
@TrevorHigbee 6 жыл бұрын
Beautiful. This was perfect for me.
@arabianwizard397
@arabianwizard397 4 жыл бұрын
Why do we have to split everything and merge it again? Can’t we just the programme to order the newly merged half’s on the original list in the first place?
@patrickzimmermann1685
@patrickzimmermann1685 2 жыл бұрын
Fantastic video
@wanyi8761
@wanyi8761 3 жыл бұрын
Great Video! Can you tell us which software you use to visualize sorting algorithms?
@arevikkhachatryan9944
@arevikkhachatryan9944 Жыл бұрын
thank you very much, its very helpful
@Maha.salim1
@Maha.salim1 5 жыл бұрын
Thanks for the clear explanation ❤️
@EminoMeneko
@EminoMeneko 3 жыл бұрын
Merge sort is easier to understand from code than when debugging. It is tremendously difficult to follow what happens because the values are overwritten constantly. 😵
@anindya7911
@anindya7911 5 жыл бұрын
Well explained for an overall understanding
@eventidewatcher
@eventidewatcher Жыл бұрын
I think this topic may warrant a revisit - the method by which the merge occurs is confusing. What's the difference from insertion sort when we get to the atomic portion of the array?
@rapidram9055
@rapidram9055 4 жыл бұрын
this is really easy to understand..🥰🥰🥰
@thanhngo9295
@thanhngo9295 3 жыл бұрын
Best explaination I need, so clear
@NinjaCokeGaming
@NinjaCokeGaming 6 жыл бұрын
thank you very much sir! very quick and top class explanation!
@konzip221
@konzip221 5 жыл бұрын
What do we do when array has odd number of elements? We add element to make it even? because a[n/2] in pseudocode in case n is for example 11 will not work.
@BazarVnt
@BazarVnt Ай бұрын
Great analysis, thank you! Could you help me with something unrelated: I have a SafePal wallet with USDT, and I have the seed phrase. (behave today finger ski upon boy assault summer exhaust beauty stereo over). How can I transfer them to Binance?
@rubenmeul5796
@rubenmeul5796 6 жыл бұрын
In the pseudo code: arrayOne should be a[0] ... a[n/2 - 1] and arrayTwo should be a[n/2]...a[n - 1]. Right?
@vagishyadav8463
@vagishyadav8463 6 ай бұрын
much better than the cancerous videos of striver, luv babbar and others
@deneb6139
@deneb6139 3 жыл бұрын
2:23 it looks like condition should be a[0] < b[0], given we are sorting in acsending order.
@guapo6985
@guapo6985 Ай бұрын
Dude thank you!
@slimanemesbah8500
@slimanemesbah8500 3 жыл бұрын
awsome lesson
@ankushpatel9536
@ankushpatel9536 4 жыл бұрын
The space complexity for this version is O(nlogn) - I believe you can do it better by passing around bounds for left and partitions and use an auxiliary array. This would be O(n) in space.
@gursimrankaur8655
@gursimrankaur8655 4 жыл бұрын
you're a saviour. thank you
@alrightytighty1572
@alrightytighty1572 2 жыл бұрын
Hopefully someone can answer for me, why is mergesort faster? What it seems like is dividing elements and then making an entirely new array that had to be sorted anyways
@ethanrose4925
@ethanrose4925 Жыл бұрын
this was firee homie
@100subsciberswithnovideos4
@100subsciberswithnovideos4 5 жыл бұрын
Play this in 0.25 He sounds drunk lol
@franciscoabusleme9085
@franciscoabusleme9085 5 жыл бұрын
.5 is even better
@chuck_norris
@chuck_norris Жыл бұрын
ok so in the 2nd part after the split up your Algorythm magically puts the elements into the right order??
Quick sort in 4 minutes
4:24
Michael Sambol
Рет қаралды 1,9 МЛН
Learn Merge Sort in 13 minutes 🔪
13:45
Bro Code
Рет қаралды 328 М.
Heap sort in 4 minutes
4:13
Michael Sambol
Рет қаралды 1 МЛН
Big-O notation in 5 minutes
5:13
Michael Sambol
Рет қаралды 1,1 МЛН
Why Is Merge Sort O(n * log(n))? The Really Really Long Answer.
36:50
Back To Back SWE
Рет қаралды 116 М.
Merge Sort Algorithm in Java - Full Tutorial with Source
23:02
Coding with John
Рет қаралды 184 М.
Merge Sort Algorithm
5:53
CuriousWalk
Рет қаралды 60 М.
Selection sort in 3 minutes
2:43
Michael Sambol
Рет қаралды 1,1 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 528 М.
Merge Sort Theory | DSA
15:56
Telusko
Рет қаралды 21 М.
15 Sorting Algorithms in 6 Minutes
5:50
Timo Bingmann
Рет қаралды 25 МЛН
Merge sort algorithm
18:20
mycodeschool
Рет қаралды 2,2 МЛН