Merge Sort Algorithm in C++ Programming | (C++ Program) | Part - 2 | Sorting Algorithms - DSA

  Рет қаралды 89,382

Simple Snippets

Simple Snippets

4 жыл бұрын

Support Simple Snippets by Donations -
Google Pay UPI ID - tanmaysakpal11@okicici
PayPal - paypal.me/tanmaysakpal11
--------------------------------------------------------------------------------------------- Implement merge sort sorting algorithm using C++ programming language. In this tutorial we will write a program to implement merge sort algorithm.
If you don't know merge sort algorithm working, please check the the part - 1 where we discuss the working of merge sort algorithm.
Full DSA playlist - • Introduction to Data S...
Full Code & Theory article - simplesnippets.tech/merge-sor...
C++ Programming Tutorials for Beginners Course - • Introduction to Comput...
Simple Snippets Official Website -
simplesnippets.tech/
Simple Snippets on Facebook -
/ simplesnippets
Simple Snippets on Instagram -
/ simplesnippets
Simple Snippets on Twitter -
/ simplesnippet
Simple Snippets Google Plus Page -
plus.google.com/+SimpleSnippets
Simple Snippets email ID -
simplesnippetsinfo@gmail.com
For More Technology News, Latest Updates and Blog articles visit our Official Website - simplesnippets.tech/
#mergesort #sortingalogrithms #dsa #divideandconcquer #algorithms

Пікірлер: 129
@oshinsaini255
@oshinsaini255 3 жыл бұрын
I now realize that I never really understood merge sort until I saw your video. Thank you for such a wonderful explanation :)
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Happy to hear that! Please support me by sharing the videos and our channel with your friends too. Thats the biggest help and support you can provide 😇
@Pr3kashSingh
@Pr3kashSingh 2 жыл бұрын
So far one of the best explanation as compared to videos of other channels.
@anshipatel9703
@anshipatel9703 2 жыл бұрын
After watching both parts I'm pretty much confident about merge sort...❤✌
@majgenscjain9991
@majgenscjain9991 2 жыл бұрын
Excellent presentation by the speaker. Very immaculately and interestingly explained the very intrricate and complex concepts.
@mannnanshaikh7608
@mannnanshaikh7608 Жыл бұрын
u teach better than Apna College(They just read the code from google and write in the videos lol).
@SimpleSnippets
@SimpleSnippets Жыл бұрын
Thank you bro ❤️😊 happy to know you like my teaching style 👍
@simransinha6225
@simransinha6225 3 жыл бұрын
So far one of the best explanation as compared to videos of other channels.Keep it up👍
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Thats really great to know Simran🙏 Please support me by sharing the videos and our channel with your friends too. Thats the biggest help and support you can provide 😇
@kaushalyadissanayake6559
@kaushalyadissanayake6559 3 жыл бұрын
Thank you thank you thank you so much ❤❤ your explanation is very clear. I have never seen such a clear merge sort vedio. I'm ur neighbor Srilankan
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Glad it was helpful!
@shinzo007
@shinzo007 5 ай бұрын
Best video on the merge sorting All the queries are clear 💯💯
@meghamalusare1203
@meghamalusare1203 2 жыл бұрын
You are very good teacher. I only think I will do coding but I never did. But after watching your videos I am doing well. Thank you so much
@dev754
@dev754 2 жыл бұрын
Bhai your content is superior than over hyped channels. The way you explain is more than paid courses . You deserve more
@SimpleSnippets
@SimpleSnippets 2 жыл бұрын
Thank You so much bro ✌️ As Baburao said - Tu toh Dev manus nikla 🙏😂❤️
@floriskruger8098
@floriskruger8098 2 жыл бұрын
Thank you so much sir!!!!! The diagram came in clutch!
@dheanimates
@dheanimates Жыл бұрын
My man explained it like butter 💥
@devamjoshi4180
@devamjoshi4180 3 жыл бұрын
How to do merge sort of an 2D array of co-ordinate points?
@gaminghouse223
@gaminghouse223 23 күн бұрын
thanks for the valuable lesson :)
@yogeshchauhan9401
@yogeshchauhan9401 7 ай бұрын
You have hardcodes 5 in temp array but if we dont know the size of arr how will we do it ?
@mohammad_zrar
@mohammad_zrar Жыл бұрын
pretty helpful thank you a lot, master ❤
@owais_islamic_studio
@owais_islamic_studio Жыл бұрын
such a worder ful video Thanks for this video
@Syard4250
@Syard4250 Жыл бұрын
well explained, thanks!
@schwaad9655
@schwaad9655 2 ай бұрын
really good video! wasn't understanding the sorting part of the algorithm at first, looking at other tutorials, but with your explanation i finally get it
@SimpleSnippets
@SimpleSnippets 2 ай бұрын
Glad it helped!
@mansigoyal4796
@mansigoyal4796 3 жыл бұрын
Thank you so much sir. You made engineering quite easy for me :)
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
You are most welcome
@muzammalhussain4887
@muzammalhussain4887 Жыл бұрын
in merge() function, where declaring an array of interger temp[5], this should be int temp[l-r] correct me if i'm wrong.
@masumali8356
@masumali8356 Жыл бұрын
You are the best.....masum
@vijaybhame6773
@vijaybhame6773 2 жыл бұрын
Best Explanation...........
@preethinayak9659
@preethinayak9659 2 жыл бұрын
Sir why we are using mergesort function as recursive
@nagasudheer8098
@nagasudheer8098 2 жыл бұрын
Amazing, Understood about Merge sort and i think you missed out time and space complexity analysis....Thank you!!!
@MKSundaram
@MKSundaram 3 жыл бұрын
Will Merge Sort sort 2D Matrix?
@ekramulnibir
@ekramulnibir 2 жыл бұрын
why uve declared size=(r-1)+1; in line no 13?
@iyswaryabala4283
@iyswaryabala4283 2 жыл бұрын
in output "after merge sort" it's not sorting properly.i don't the reason.anyone can pls say a solution .
@nitismita1035
@nitismita1035 2 жыл бұрын
Awesome 👌
@salmanmugharbel
@salmanmugharbel Ай бұрын
you are the best ever I said that and I engineering maybe you the second one I say that for him
@ibrahemnjr9924
@ibrahemnjr9924 2 жыл бұрын
You are a legend 💕
@kylorojen8066
@kylorojen8066 2 жыл бұрын
Hello sir quick question. I have a problem with this following codes because this lot is not showing up when I compile and start it. But it doesn't say that it has errors. MergeSort(x, 0, 4); cout
@pratikmulik0049
@pratikmulik0049 Жыл бұрын
After for loop write cout
@sahilarora7186
@sahilarora7186 3 жыл бұрын
Loved it
@mohamedboubakeur4554
@mohamedboubakeur4554 8 күн бұрын
Thankyou bro
@AnujKumar-pt9tk
@AnujKumar-pt9tk 3 жыл бұрын
While running code on leetcode ,it is showing addresssanitization: error stack overflow on address......plz anyone help me with this
@user-bu8mg7uq3s
@user-bu8mg7uq3s 2 жыл бұрын
thank you so much
@saikrishnadouluri4433
@saikrishnadouluri4433 2 жыл бұрын
thanks
@aayushreejaiswal1568
@aayushreejaiswal1568 2 жыл бұрын
Your explanation is so amazing 😊🥺 CAn you please make videos on competitive programming lessons
@Sky-nt1hy
@Sky-nt1hy 4 жыл бұрын
How can you do int myarr[size]; ??? I thought the array size should be constant unless u dynamic memory allocation such as int *myarr=new int(size) How does that code even work????
@abdelrhmanahmed1378
@abdelrhmanahmed1378 3 жыл бұрын
you right thats valid only in g++ compilers like codeblocks ,because it valid in c, but in other c++ compiler you need to dynamically allocate the array in order to put dynamic size
@MaxProgramming
@MaxProgramming 4 жыл бұрын
Please make a video on designing a website with wordpress and 000 web host
@Summer-me4pt
@Summer-me4pt 4 жыл бұрын
Fantastic stuff
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
Glad you enjoyed it
@TheMrMelodramatic
@TheMrMelodramatic 3 жыл бұрын
Thanks for this! It was very meticulous and thorough. 👍🏼
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Glad it was helpful! Thank you so much buddy. It would mean a lot if you could please share our channel and videos with your friends too ✌
@Hizzus
@Hizzus 2 жыл бұрын
exellent
@emerydergro5332
@emerydergro5332 Жыл бұрын
The basic idea is actually not dividing the original vector into small vectors(no need to instantiate subvectors), but update the left, middle and right values, so the "arr" in the argument list of function "merge" is always the original one, and you always need to define the "k" variable instead of just tem.push_back()
@leggomaheggo
@leggomaheggo 2 жыл бұрын
thank you so much from the state of texas!
@SimpleSnippets
@SimpleSnippets 2 жыл бұрын
Most welcome Eric. Glad you liked the video 😊✌️
@harshittanday4001
@harshittanday4001 3 жыл бұрын
Great explanation
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Glad you think so!
@JapotjapotGalit
@JapotjapotGalit Жыл бұрын
Can i get the code to copy
@vivekmunna
@vivekmunna 4 жыл бұрын
for me dynamic allocation did not work. but when i change below 'k' assignment and temp index, it works fine ... int k =0; for(int s=l;s
@FurqaanShariff
@FurqaanShariff 3 жыл бұрын
Thank you soo much I had the same propblem. It would would work for size 3. But, when I went to size 7 it would only print values for before sorting and would exit the code. Any idea why? Dynamic way was working correctly before and after sorting for array size 3?
@shishankrawat2105
@shishankrawat2105 3 жыл бұрын
Declare the size integer as follows int b; // Stores the size of array to be used cin>>b; Main key is here : static int size = b; With the use of static keyword the size variable can be used globally. I hope it helps 🙂.
@varadkulkarni606
@varadkulkarni606 2 жыл бұрын
Declare vector in merge function instead of temp array and push numbers and keep all the code same It will definitely work .arr[s]=temp[s-l] keep it same as you mentioned.
@ribhusengupta9967
@ribhusengupta9967 2 жыл бұрын
temp[s-l] what does it means and why it is working could someone explain???
@shyamsundard5824
@shyamsundard5824 Жыл бұрын
Bro please help me.What can be done to sort the array in descending order
@abinanthanva289
@abinanthanva289 Жыл бұрын
do the same algorithm and reverse it, that's a quick fix
@codeoutput7166
@codeoutput7166 3 жыл бұрын
Thank you very much sir
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Most welcome
@meghamalusare1203
@meghamalusare1203 2 жыл бұрын
I want to join your classes. If its possible plz let me know.
@SurajSinghMIS
@SurajSinghMIS 4 жыл бұрын
This Video is not added in playlist.Please Add this video in "Data Structure and algorithms" playlist
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
Thank you very much Suraj for letting this know. I have added this video in the playlist 😇
@TheAsdasdasfasas
@TheAsdasdasfasas 3 жыл бұрын
Why is the size inside the merge function (r-1)+1, can't you directly put r instead?
@akileshkumarb7750
@akileshkumarb7750 3 жыл бұрын
( R - L ) + 1
@ZhenZhou700
@ZhenZhou700 3 жыл бұрын
For the first merge(arr[], l, m, r) function, more specifically merge(arr, 0, 0, 1), you'll notice that writing temp[r] gives you temp[1], an array with size of 1. However, this merge function requires an array of size 2 (eg. temp[2]), so this is the main reason you cannot directly write "r" as mentioned. On the other hand, writing temp[size] whereby size= r-l+1 does the job well. In addition, other than updating this dynamic size, the following changes have to be made as well. while (i
@XxDeAtHxMOnKeYxX
@XxDeAtHxMOnKeYxX 2 жыл бұрын
@@ZhenZhou700 I also made these changes and it fixed my Segmentation Fault error. I also changed 'int myarr[size]' to 'int *myarr = new int[size]' because you need to dynamically allocate the array in order to put a dynamic size: while (i
@jashwanthkumarvasampally2978
@jashwanthkumarvasampally2978 4 жыл бұрын
u r a god
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
LOL, thank you 🙏😅❤
@nomank8274
@nomank8274 Жыл бұрын
Can anyone please tell me how's this code even working? I mean to say that nothing is returned in main() so how's 'myarr' array is sorted given the fact that its scope is limited to main() only whereas sorting is done with the help of mergesort() and merge() which can't manipulate 'myarr' array
@abinanthanva289
@abinanthanva289 Жыл бұрын
when you are passing the array to mergeSort function in main(), you are actually passing the pointer itself. An array can never be passed as an argument, hence when you pass an array to a function, it's always a pointer hence the array elements are also changed if you change it within the function
@amans6504
@amans6504 3 жыл бұрын
Hi dude, i wanna ask this question. Why most CSE students in india don't do the development part? Everyone is just busy solving these ds algo, cp etc.
@vidhiangrish1456
@vidhiangrish1456 3 жыл бұрын
Because development doesn't land you a good job which is very sad because it's all development once you get the job. However, a developer must also know the useful algos but that is never enough for interviews :)
@amans6504
@amans6504 3 жыл бұрын
@@vidhiangrish1456 alright i already got the answer. I think algorithms and ds are fine, i enjoy them all but cp doesn't make sense, it feels like rat race.. Also, too much of ds algo is toxic, that'll make you eligible just for a job in MNC, this shouldn't be the goal, atleast for me, i wanna build something at the end.. job is old school thing.
@vidhiangrish1456
@vidhiangrish1456 3 жыл бұрын
@@amans6504 Trust me that's my goal too. I've been so much into development that I've never even considered going for any coding challenges/ competitions. And now knowing that it won't land me a job has got me really worried cause if it's not a good company, the developers are exploited here in India.
@amans6504
@amans6504 3 жыл бұрын
@@vidhiangrish1456 try for startups, they don't care much about ds algo thing. Also, learning Will be far better at startups, only downside is you've to add value else they'll fire you
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Ds algo cp has become overrated. Surely it's important to cover the fundamentals but yeah along with that it's more if not equally important to actually build some projects. In my recent tech talks interview with Sourav Johar(VMware employee) we discussed this point in detail. Y'all should watch the video 🤟
@abhishekbose5613
@abhishekbose5613 4 жыл бұрын
Sir if I am running this program with 7 or more elements the sorted array is not getting printed and a return value of 3221225477 is getting displayed! The code is running perfectly fine with 6 or less elements though! Where do you think I might have mistanken?
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
I think that is because we have hardcoded the size of the array as 5. you can take the size of the array from the user and then make those changes in the code and try. ✌
@abhishekbose5613
@abhishekbose5613 4 жыл бұрын
@@SimpleSnippets that is where the problem arises I haven't hard coded the values sir.. I had taken the input from the user and crossed a lot of times, but this issue isn't resolving! I tried googling out the return value also, looks like it isn't any random no. Can you help me with that? Moreover I was writing the code by looking at the video so and cross checked!
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
Do one thing, drop me a mail and I'll send you my version of code which takes size of the array at run time. Mail me at - simplesnippetsinfo@gmail.com
@abhishekbose5613
@abhishekbose5613 4 жыл бұрын
I've mailed you! Thank you so much!!!
@rajparmar1622
@rajparmar1622 4 жыл бұрын
Yeah .. im getting segmentation fault
@mohammadhaseeb948
@mohammadhaseeb948 4 жыл бұрын
Showing error for numbers greater than 7 Sir i think the size of temp array should remain constant for every recursive call and not equal to (r-l)+1
@bonzo6989
@bonzo6989 3 жыл бұрын
Yess it should be var temp = new int[arr.Length];
@bonzo6989
@bonzo6989 3 жыл бұрын
I'm going to comment on myself lool. temp = new int[arr.Length] will work but you allocate extra space. @ Vivekanandan Mullainathan gave solution here. If temp = (r - l) + 1 then k has to be 0 not l. The copy temp to original array logic changes as well.
@saikrishnamodhupalli9182
@saikrishnamodhupalli9182 4 жыл бұрын
Bro iam getting segmentation fault iam compiling in terminal
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
Some internal issue. check the code link that I have shared in the video description ✌
@venkatr2307
@venkatr2307 5 ай бұрын
I copied your code and tried running it but It didn't work
@javedakbar5375
@javedakbar5375 4 жыл бұрын
why it was not in the list?
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
What list ?
@atulnath4858
@atulnath4858 Жыл бұрын
Bro make video with hindi language so that all videos of your will be get help thanks for the explanation keep it up 👍
@aaranyaksantra9933
@aaranyaksantra9933 Жыл бұрын
🤩
@behindthescene4406
@behindthescene4406 3 жыл бұрын
bro u got a net subscriber.
@SimpleSnippets
@SimpleSnippets 3 жыл бұрын
Welcome to Simple Snippets!
@Monkey_D_Luffy0001
@Monkey_D_Luffy0001 3 ай бұрын
A rrrrrrrrrrrr😂🤣...nice video btw
@vinaykumar9688
@vinaykumar9688 5 ай бұрын
This is the best explanation , hat's of you brother 🫡
@Keshish4
@Keshish4 4 жыл бұрын
gg usefull
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
THank you. Please do share the videos with your friends too 😇
@everythingcelebrity6296
@everythingcelebrity6296 4 жыл бұрын
Dude also make a video on hashing
@SimpleSnippets
@SimpleSnippets 4 жыл бұрын
Yes I will ✌
@sharifajmir
@sharifajmir 3 жыл бұрын
I just stop video and suscribe execpt i could do any thing
@aliajmal8517
@aliajmal8517 2 жыл бұрын
In Merge function the size of temp array could be r+1 (int temp[r+1]). It's more efficient. Anyhow! learnt alot from this channel. Thank you sir your efforts that you are putting into.
@khurramsaeed1999
@khurramsaeed1999 2 жыл бұрын
PLEASE USE DARKMODE PLEASE
@prabaljainn
@prabaljainn 4 жыл бұрын
Instead of saying "I think it works, please say that the "scope is limited". 8 : 54
@maitreyshukla258
@maitreyshukla258 3 жыл бұрын
wow
@prabaljainn
@prabaljainn 3 жыл бұрын
@@maitreyshukla258 shukla Ji yaha Bhi
@anas_tabassum
@anas_tabassum 3 жыл бұрын
Can anyone explain that where swapping is happening at sub levels 🤔
@TheMrMelodramatic
@TheMrMelodramatic 3 жыл бұрын
12:14 So within the merge() function there is a temporary array, temp, where we place the lower of arr[i] and arr[j]. There's not really any swapping going on because we are placing the values into a new array. After the sorting, we set the original array equal to the temp array at line 42 @ 18:00
@issanijem5248
@issanijem5248 2 жыл бұрын
cos imak
@GeorgeFlorian12
@GeorgeFlorian12 3 жыл бұрын
Inside `merge()` you can't allocate the size of the array the way you did because it will return an error: int size = (r - l) + 1; int temp[size]; …won't compile because of the following error: "expression must have a constant value of variable (declared at line #) cannot be used as a constant"
@ashishbhardwaj9693
@ashishbhardwaj9693 3 жыл бұрын
for me it compiled for smaller array but it also gives segmentation fault for big array size not a good idea to use.
@ribhusengupta9967
@ribhusengupta9967 2 жыл бұрын
Yes Me too...Better to put some large constant value...it wont take input size in c++ 17 ...Different compiler acts differently...mine and yours compiler are same
@mohdfaraz4306
@mohdfaraz4306 Жыл бұрын
you have to give size as argument in all the function calls and function argument. only then the merge sort can get the value of size. mergesort(myarr,0,size-1,size) like this
@ahmadferozarshad1354
@ahmadferozarshad1354 Жыл бұрын
Bhai Jaan plzzz Urdu men perhaya karen🙏🙏🙏🙏
@bruhbruh8137
@bruhbruh8137 3 жыл бұрын
the code is totally wrong, you try to insert 9 7 5 8 6.
@adeniyiadeniran6029
@adeniyiadeniran6029 3 жыл бұрын
yeah it does not work for all input and even negative numbers
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 501 М.
Tom & Jerry !! 😂😂
00:59
Tibo InShape
Рет қаралды 64 МЛН
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 94 МЛН
every good programmer should know how to code this data structure (its easy)
21:08
Merge Sort | C Programming Example
18:02
Portfolio Courses
Рет қаралды 72 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 612 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 311 М.
100+ Linux Things you Need to Know
12:23
Fireship
Рет қаралды 218 М.
Learn Merge Sort in 13 minutes 🔪
13:45
Bro Code
Рет қаралды 266 М.
#027 [Data Structures] - Merge Sort Algorithm With Implementation
19:04
Merge sort algorithm
18:20
mycodeschool
Рет қаралды 2,2 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 501 М.