Sort An array using Recursion

  Рет қаралды 243,038

Aditya Verma

Aditya Verma

Күн бұрын

Sorting and array using Recursion by using Induction-Hypothesis and Base condition Approach.
Pdf notes and code: / 38605857 .
------------------------------------------------------------------------------------------
Here are some of the gears that I use almost everyday:
🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
💻 : My gaming laptop: amzn.to/3yjcn23
📱 : My Ipad: amzn.to/39yEMGS
✏️ : My Apple Pencil: amzn.to/3kMnKYf
🎧 : My Headphones: amzn.to/3kMOzM7
💺 : My Chair: amzn.to/385weqR
🛋 : My Table: amzn.to/3kMohtd
⏰ : My Clock: amzn.to/3slFUV3
🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

Пікірлер: 466
@nityanandbhaskar2155
@nityanandbhaskar2155 4 жыл бұрын
I can't thank you enough for making these videos. I completed yours dynamic programming playlist and gained so much knowledge. I was once rejected in interview because i didn't know how to approach DP problems. But after watching your videos , I gained massive confidence and also got placed. Thanks a lot
@willturner3440
@willturner3440 4 жыл бұрын
Amazon
@amanjyotiparida5818
@amanjyotiparida5818 4 жыл бұрын
@Himalayan drifter google
@akashbhadouria6727
@akashbhadouria6727 3 жыл бұрын
@Himalayan drifter bhindi market
@Lalit_Shirsath
@Lalit_Shirsath 3 жыл бұрын
@@akashbhadouria6727 😂
@trackbackresearch
@trackbackresearch 3 жыл бұрын
@@Lalit_Shirsath System Administrator at Thakur Prasad Sao & Sons Pvt. Ltd.
@varungoel6981
@varungoel6981 3 жыл бұрын
Today I got selected in Flipkart all because of your videos Aditya! Besides learning technical stuff, I got to learn from you on how to handle and approach complex problems. Thanks a lot! I really owe you a lot, can't msg on LinkedIn since you haven't accepted my request but I'm gonna ping you as soon as I join to thank you :)
@TheAdityaVerma
@TheAdityaVerma 3 жыл бұрын
Welcome onboard buddy 😅❤️ Glad I could help you in our journey To Flipkart !!
@shinosukenohara.123
@shinosukenohara.123 2 жыл бұрын
pleae guide
@amiyamishra9858
@amiyamishra9858 2 жыл бұрын
@@TheAdityaVerma please make payslist on tree and graph as well ,Your teaching style helps to grasp the concept easily & to remember it for a long time.Thank you !!
@tarunstv796
@tarunstv796 2 жыл бұрын
Thank you brother!!!
@gametech7046
@gametech7046 2 жыл бұрын
Every man must bow, every tongue must confess, this is the best recursion tutorial and Aditya is the greatest teacher of all time.
@rishabhsahu3303
@rishabhsahu3303 4 жыл бұрын
Boss Graph ki playlist ayegi na to lagra apka channel alag hi level pai chala jayega (graph k hard questions include karna )
@vaisnavichirag
@vaisnavichirag 3 жыл бұрын
It has been one year may be you were placed somewhere but the playlist didn't come yet😅😅😅
@vishalramnani5884
@vishalramnani5884 3 жыл бұрын
@@vaisnavichirag aur bhai? xD
@ru2979
@ru2979 3 жыл бұрын
@@vaisnavichirag student of Sri Aurobindo Vidyamandir, U remember me ?
@111rhishishranjan2
@111rhishishranjan2 2 жыл бұрын
its been 2 yrs still that series didn't come . Aditya Verma bhiya where are you ???????
@matrixtoogood5601
@matrixtoogood5601 3 жыл бұрын
At 17:44, we can also use a binary search (recursive/iterative) to find the position in O(logN) since the left subarray is sorted. This is the most optimized version of insertion sort using recursion. Amazing explanation!
@amitmahato4876
@amitmahato4876 3 жыл бұрын
Yet, the overall time complexity will be O(n^2)
@matrixtoogood5601
@matrixtoogood5601 3 жыл бұрын
@@amitmahato4876 Practically, complexity is only the first thing you check. You have to improve overall performance of the code and asymptotic complexity is only a broad-level analysis. My code is O(N^2) but it will still be much faster than yours :)
@practiceTech
@practiceTech 2 жыл бұрын
@@matrixtoogood5601 You r right bro
@vetlakvsatyanarayana746
@vetlakvsatyanarayana746 Жыл бұрын
@@matrixtoogood5601 share your code once bro
@trueresources3847
@trueresources3847 Жыл бұрын
no man if to insert an element, in the worst case you can have have to shift all elements to make place for the given element, this is not about finding an element, it is about inserting an element in-place in a array or vector.
@habibhasanshakil9086
@habibhasanshakil9086 2 жыл бұрын
#include using namespace std; void Insert(vector &v, int temp){ if(v.size() == 0 || v[v.size()-1]
@nirbhaychopra7762
@nirbhaychopra7762 2 жыл бұрын
Thanks
@imPriyansh77
@imPriyansh77 Жыл бұрын
Thanks !
@abhinavennala9613
@abhinavennala9613 4 ай бұрын
Thanks man!
@hiteshsondhi
@hiteshsondhi 3 жыл бұрын
I always struggled thinking about recursion and how it keep tracks of the variable state and kind of lost in that space. But the way you explained makes it look a hell easier now with focused mind. Thanks Aditya
@gauravraj2604
@gauravraj2604 3 жыл бұрын
I don't have words to explain how I am feeling after going through this video. Never ever thought of sorting an array using recursion. I cannot thank you enough Aditya... Every video I watched of urs taught me a new concept...
@harshitbhatt5875
@harshitbhatt5875 3 жыл бұрын
Finally...a guy who knows what he's teaching.
@rajatjakhar3146
@rajatjakhar3146 2 жыл бұрын
@theprogrammer change array to vector
@abhinandanmishra4391
@abhinandanmishra4391 Жыл бұрын
i think this is the best playlist of recursion i have seen i think this is last becoz explained this is very simple manner of how to think about any recursive function and how to implement that thank you sir for such a great video
@hrishabhdigaari9140
@hrishabhdigaari9140 4 жыл бұрын
Hey Aditiya, love your content. It's very good. I have been following you for a month now. I have a suggestion for you, If it is possible then in the description of every video, you should put at least 5 similar questions, based on the concept that you taught, from geek for geeks. That would really enhance our practice multiple folds.
@NehaGupta-lf1sr
@NehaGupta-lf1sr 4 жыл бұрын
I agree
@devashishvyas9608
@devashishvyas9608 3 жыл бұрын
agreed that will definitely increase the outcome of the video. This will help us to apply the concept by ourself in various question. You don't have to put the questions in every video just put them in the videos which are very important.
@ashvinimeshram5242
@ashvinimeshram5242 3 жыл бұрын
Your way of teaching is just awesome. Thanks for your efforts.
@sachetansabhahit6235
@sachetansabhahit6235 4 жыл бұрын
Java code for recursiely sorting an arrar: public class Main { public static void main(String[] args) { ArrayList arr = new ArrayList( Arrays.asList(3,2,1,4,5,6,7) ); arr = sort(arr); System.out.println(arr); } public static ArrayList sort(ArrayList arr){ if(arr.size()==1){ return arr; } int temp = (int)arr.get(arr.size()-1); arr.remove(arr.size()-1); arr = sort(arr); arr = insert(arr,temp); return arr; } public static ArrayList insert(ArrayList arr,int temp){ if(arr.size()==0 ){//|| (int)arr.get(arr.size()-1)temp){ arr.add(temp); return arr; }else if((int)arr.get(0)>=temp){ arr.add(0,temp); return arr; }else if((int)arr.get(arr.size()-1)
@ruduchabagamer5982
@ruduchabagamer5982 3 жыл бұрын
thanks
@vanditdubey8346
@vanditdubey8346 2 жыл бұрын
Thanks man video wasn't clear to me but I'll figure it out form the code
@livelypooja
@livelypooja 2 жыл бұрын
Thanks for this.now I got the clear picture 👍
@vishalghanghav748
@vishalghanghav748 2 жыл бұрын
Thanks I tried by myself ,and couldn't figure out if should use a list or not
@SILENT_4_M_6551
@SILENT_4_M_6551 Жыл бұрын
thank you bro
@123sigalit
@123sigalit 4 жыл бұрын
Aditya I like you explanations and thank you.. I noticed in this video that you started with the question of "sorting an ARRAY" and by the time you started discussing insert function you moved to speaking about sorting a vector.
@rishabhsingh5816
@rishabhsingh5816 4 жыл бұрын
no paid content can also teach us this awesomely as u do.. bhai next series on backtracking
@creativegiant148
@creativegiant148 4 жыл бұрын
Bro i will never hesitate calling u the best teacher of recursion
@nishantgupta6902
@nishantgupta6902 Жыл бұрын
code: #include using namespace std; #include void insert(vector &v, int value){ if(v.size()==0 || v[v.size()-1]
@sasiraj1594
@sasiraj1594 4 жыл бұрын
This is Insertion Sort using Recursion :)
@settyruthvik6236
@settyruthvik6236 3 жыл бұрын
yes
@radium990
@radium990 2 ай бұрын
import java.util.ArrayList; import java.util.List; public class RecursiveSort { public static void recursiveSort(List array) { // Base condition: An array of size 0 or 1 is already sorted if (array.size()
@udaypandey5659
@udaypandey5659 2 жыл бұрын
The best way to learn is : while Mr. Verma is teaching parallelly you also use pen paper and write . Then you will get very clear picture. I watched 2 times, but it was blur 50 -60 % .. I took pen paper and wrote then Trust me : in just one shot i typed the entire code. Thanks Aditya!
@ganeshkhirwadkar4127
@ganeshkhirwadkar4127 3 жыл бұрын
Excellent explanation with full patience !!!
@MilindGupta
@MilindGupta 3 жыл бұрын
Hey can you tell me in short what does hypothesis and induction means?
@ganeshkhirwadkar4127
@ganeshkhirwadkar4127 3 жыл бұрын
@@MilindGupta I will try my best to explain : Hypothesis - Call your function but with smaller valid input, you do it to make sure if your function works let suppose for N then it will work for N-1. Recursively it will work till that smallest valid input and gives you desired output. Induction - After your hypothesis returns you a valid output then you can logically do some other stuff in induction. Base Condition - This condition will ensure you reach that valid smallest input which you thought of and then you can start returning from there ! It is very crucial part in code, if you miss this then your code can end up in infinite recursive calls and can lead to memory overflow. Hope this will help to some extent otherwise @Aditya Verma is here to rescue :)
@kushalappaca5324
@kushalappaca5324 3 жыл бұрын
I implemented bubble sort in recursion.... Thanks to you sir..
@sidharthpanda8557
@sidharthpanda8557 4 жыл бұрын
for those who are in need of Python code :- arr = [2, 3, 5, 4, 10, 3, 4, 5, 2, 1, -10, 0] class Sorting: def insert(self, arr, temp): if len(arr) == 0 or arr[len(arr) - 1]
@prativadas4794
@prativadas4794 3 жыл бұрын
this question may seem silly, but when i tried your code and made a change where i defined n=len(arr) and replace len(arr) with n, and i get the error: list index out of range. can you tell me why is that?
@adhirajmajumder
@adhirajmajumder 2 жыл бұрын
@@prativadas4794 class Sorting: def insert(self,arr,temp): n=len(arr) if n==0 or arr[n-1]
@rahulnagwanshi2348
@rahulnagwanshi2348 2 жыл бұрын
i have the same code but in gfg this in only passing two test cases :/
@fuzi_blossom
@fuzi_blossom Жыл бұрын
//Thanku Aditya sir -----> for removing the fear of recursion from our head #include using namespace std; void insert(vector&a,int k){ int n=a.size(); //base case if(n==0 || a[n-1]
@deeproy7292
@deeproy7292 4 жыл бұрын
code for this in java(try to solve and then refer, also let me know if any error is there) import java.util.*; public class MyClass { public static void main(String args[]) { LinkedList ll = new LinkedList(); ll.add(2); ll.add(1); ll.add(-7); ll.add(5); ll.add(33); System.out.println(ll); sort(ll); System.out.println(ll); } static void sort(LinkedList ll){ if(ll.size() == 1) return; int lastEement = ll.removeLast(); sort(ll); insert(ll, lastEement); } static void insert(LinkedList ll, int element){ if(ll.size() == 0 || ll.getLast()
@TeluguTekTeach
@TeluguTekTeach 2 жыл бұрын
After a long time .. after seeing this, maza aaya.. Thanks for teaching in a different way
@simply_akh1l
@simply_akh1l 3 ай бұрын
Thanks, understood
@SachinKumar-ut4gn
@SachinKumar-ut4gn 4 жыл бұрын
sir program k sath uska time complexity v discuss kr dia kro plz... rest all is awesome😍😍😍😍
@vishalc6658
@vishalc6658 4 жыл бұрын
What would be the time complexity of this problem? Anyone wants to guess?
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
@@vishalc6658 O(n^2) i guess
@shubhamrawat7895
@shubhamrawat7895 4 жыл бұрын
That's an insertion sort.. With recursion stack space complexity
@snigdhobhattacharya4697
@snigdhobhattacharya4697 4 жыл бұрын
This is one of the most conceptual videos I have ever seen. Kudos to you, man!
@shishirchaurasiya3435
@shishirchaurasiya3435 2 жыл бұрын
Just wanna to say l0ve you Aditya Verma.... what confidence I gained in this question was just awesome......... BiG tHanks
@laxmangurjar4776
@laxmangurjar4776 3 жыл бұрын
bhai msst padhata hai tu sach mai. ek alag way mai sochna sikhaata hai . thanks yaar
@deepanshuverma6237
@deepanshuverma6237 7 ай бұрын
Java code : public class SortArray { public static void main(String[] args) { List list = new ArrayList(Arrays.asList(3,2,1,4,5,6,7)); System.out.println(sort(list)); } public static List sort(List list) { if (list.size() == 1) return list; int temp = list.remove(list.size() - 1); sort(list); insert(list, temp); return list; } public static List insert(List list, int temp) { if (list.isEmpty() || list.get(list.size() - 1)
@nileshkumar9679
@nileshkumar9679 4 жыл бұрын
Happy Teachers days Bhaiya ❤️❤️❤️❤️❤️really you are one of best Teacher please make more video on graph and others stuff for placement
@abhilakhchaudhary5122
@abhilakhchaudhary5122 3 жыл бұрын
@abhilakhchaudhary5122
@abhilakhchaudhary5122 3 жыл бұрын
3
@dimpleshah6538
@dimpleshah6538 3 жыл бұрын
I did got Time Limit Exceeded when I submitted the code for this approach on Leetcode. But the satisfaction lied in the fact that I understood recursion, implemented it and wrote the code on my own. Thanks a lot for an amazing content! :)
@sudarshansingh1072
@sudarshansingh1072 3 жыл бұрын
Recursion always need optimization..follow his DP series..
@adityakumarpandey5817
@adityakumarpandey5817 3 жыл бұрын
void insort(vector&v, int temp){ if(v.size()==0 || v[v.size()-1]< temp){ v.push_back(temp); return; } int value=v[v.size()-1]; v.pop_back(); insort(v,temp); v.push_back(value); } void sort(vector&v){ if(v.size()==0) return; int temp=v[v.size()-1]; v.pop_back(); sort(v); insort(v, temp); } int main() { vectorv; v.push_back(2); v.push_back(1); v.push_back(5); v.push_back(4); sort(v); for(int i=0; i
@tanujkasal
@tanujkasal 3 жыл бұрын
thanks for such an amazing explanation, Here's the python code for this problem: def arr_insert(a,b): if len(a)==0 or b>=a[len(a)-1]: a.append(b) return temp=a.pop() arr_insert(a,b) a.append(temp) def arr_sort(a): if len(a)
@DivitMukhi
@DivitMukhi 2 жыл бұрын
how abt bubble sort or selection sort using length=len(are)
@aniketsthings
@aniketsthings 3 жыл бұрын
This was a tuff video, it means we are growing.🔥
@ps7072
@ps7072 4 жыл бұрын
12:14 Recursive leap of faith
@workandearnwithshubhi9050
@workandearnwithshubhi9050 3 жыл бұрын
Assasin's Creed ke side effects!!!!
@YashTrivedi-iq3ft
@YashTrivedi-iq3ft 8 ай бұрын
@@workandearnwithshubhi9050 nah its miles morals (spiderman)effect
@_HRhackjack
@_HRhackjack 2 жыл бұрын
Loved this, the flow 🌞
@_HRhackjack
@_HRhackjack 2 жыл бұрын
Thanks a lot...
@harmanpreetsingh8088
@harmanpreetsingh8088 3 жыл бұрын
God-Level Explanation
@sukdipkar8108
@sukdipkar8108 2 жыл бұрын
Your thinking is next level..highly appreciated
@tushargogiya4017
@tushargogiya4017 Жыл бұрын
C++ Code #include #include using namespace std; void insert(vector&v,int ele) { int n=v.size(); if(n==0 || v[n-1]
@shashankarora
@shashankarora Жыл бұрын
Aditya you really helped me to grasp the concept of recursion. I know recursion but after certain period of time I found myself in a position where I was not able to create a working code using recursion. Yes, I will continue to see your further lecture on recursion. I am sharing the code which you explained in this video, so that other people can visualize more by running code on terminal. Understand the recursion flow: #include using namespace std; void InsertElem(vector &v, int tmp) { cout
@yashsingavi5167
@yashsingavi5167 6 ай бұрын
leetcode ka example poora dry run karke khud se samajh aaya code ka logic thank you bhai !!
@carl4705
@carl4705 28 күн бұрын
Bro jo aapne smjhaaya hai na aaj using recursion by chunks Hatts off bro Thankyou so much
@adarshsrivastava9428
@adarshsrivastava9428 3 жыл бұрын
"Increasing, Decreasing, Ascending, Descending Orders" are relative. That's a great point to note. Sometimes, we know these observations but neglect them solving the problem. Awesome Observation Sir...🙌🙌
@MilindGupta
@MilindGupta 3 жыл бұрын
Hey can you tell me in short what does hypothesis and induction means?
@sachinmaurya3259
@sachinmaurya3259 3 жыл бұрын
@@MilindGupta hypothesis is time when we write our main function and induction is where we decided how do we want to print our function example in "print the array from 1 to n" we solve it in two ways first printing from "1 to n" and then "n to 1" .... if you have watched both the video then you must have seen that just by changing the print position we were able to print it differently . So that's what the induction is Hope this help!
@123akash121
@123akash121 3 жыл бұрын
bro, you are god of programming
@raviyadav2552
@raviyadav2552 5 ай бұрын
this really feels like magic
@hemantmangwani1006
@hemantmangwani1006 4 жыл бұрын
31:29 Subscribe wagera karna he to kaar lena . He knows he is creating a difference.
@AnkushKumar-mk8ns
@AnkushKumar-mk8ns 4 жыл бұрын
Very very very informative and doubt clearing problem. Thank you aditya bhaiya😍
@ashirbadbehera5544
@ashirbadbehera5544 4 жыл бұрын
please make a video graphs as well, your way of teaching is amazing
@kumkumsharma5742
@kumkumsharma5742 4 жыл бұрын
Thanks for amazing explanation :) Kudos to learning recursion. Enclosed java Solution: package recursion; import java.util.ArrayList; import java.util.List; public class SortAnArray { public static void main(String[] args) { List l = new ArrayList(); l.add(1); l.add(0); l.add(5); l.add(2); l = sort(l); for(int i : l) System.out.println(i); } private static List sort(List l) { if(l.size() == 1) return l; //hypothesis Integer temp = l.get(l.size()-1); l.remove(l.size()-1); l = sort(l); //induction return l = insert(l, temp); } private static List insert(List l, int i) { if(l.size() == 0 || i >= l.get(l.size()-1)) { l.add(i); return l; } //hypothesis int val = l.get(l.size() - 1); l.remove(l.size()-1); l = insert(l, i); //induction l.add(val); return l; } }
@utsavseth6573
@utsavseth6573 Жыл бұрын
This is some next level content.
@aakritisingh399
@aakritisingh399 3 жыл бұрын
Code for C++: #include #include using namespace std; void sortArray(vector&); void insertTemp(vector&,int); int main() { int n; cin>>n; vectorarr; for(int i=0; i>val; arr.push_back(val); } sortArray(arr); for(int i=0; i
@ManishKumarSahu-jd2ww
@ManishKumarSahu-jd2ww 3 жыл бұрын
bro, this code didnt work on my compiler
@aakritisingh399
@aakritisingh399 3 жыл бұрын
@@ManishKumarSahu-jd2ww Is it showing any error or not giving correct output ? Because I checked it again it's working. 1. If you are running it on online compiler (onlinegdb) then do select language as C++. 2. First enter size of the array -> then enter elements of the array. It will work. Hope this sorts your problem.
@ManishKumarSahu-jd2ww
@ManishKumarSahu-jd2ww 3 жыл бұрын
@@aakritisingh399 I was running it on vscode IDE. I tried many times, and even did some variations, but didn't work, everytime am getting wrong output😭. And also the stack question (just after this one) is giving wrong output, lol. I would be glad, if you could help with that one as well :) I would try to run it on online IDE once. Thank you so much for your suggestion!
@javi2082
@javi2082 4 жыл бұрын
Hi Aditya, your Videos are great and they are helping me a lot. Please keep on posting more content on your channel and interesting problems.
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Thank you Vatsala, I will !!
@shreyasabd4974
@shreyasabd4974 3 жыл бұрын
Killer content. Man I regret being this late🥴
@deekshakarkaulia145
@deekshakarkaulia145 Жыл бұрын
hands down to your teaching skills🙌
@sajankumarkar8237
@sajankumarkar8237 Жыл бұрын
I actually thought of another method to sort recursively and its relatively simpler. You work the other way round. you go sorting the array from the 1st element. So, the hypothesis becomes sort(a)-> a[0] + sort(a[1] to a[n-1]). Now in this case the base condition becomes the stage in which you hit the last element of the array cuz there are no more elements to sort. To form the induction you have the 1st and the 2nd element of the array. if the 1st elem > 2nd elem then before removing a[0] and sorting the rest of the array you swap a[0] and a[1] so that its sorted till the 1st position. void sortr(vector &a, int i){ if(i==a.size()-1) return; if(a[i]>a[i+1]) swap(a[i], a[i+1]); sortr(a, i+1); }
@annielouise1141
@annielouise1141 Жыл бұрын
it won't workk
@annielouise1141
@annielouise1141 Жыл бұрын
#include #include #include using namespace std; void sortr(vector &a, int i){ if(i==a.size()-1) return; if(a[i]>a[i+1]) swap(a[i], a[i+1]); sortr(a, i+1); } int main() { vectora={5,4,3,2,1}; sortr(a,0); for(auto it:a) cout
@rajverma7088
@rajverma7088 10 ай бұрын
9:23
@abhishekjha1064
@abhishekjha1064 4 жыл бұрын
What an explanation man!
@harshupreti1526
@harshupreti1526 2 жыл бұрын
What about i find the max element in the array and swap it with the last element and then call the sorty fn again this time with size of array -1 as parameter ?
@thinkingmad1685
@thinkingmad1685 3 жыл бұрын
Bhai bohot bohot dhanyavaad, mujhe bohot ache se samaj aagaya yeh rha mera code . Yeh bas mere reference keliye kuch galat hoto crct karo bhaiya.... #include using namespace std; Void insert(vector &v, int val) { //base case if(v.size() ==0 || v[v.size()-1]
@vishakhachauhan6642
@vishakhachauhan6642 4 жыл бұрын
code in c: #include void insert(int *a,int temp,int size) { if(size==0|| a[size-1]
@shreymehrotra5408
@shreymehrotra5408 4 жыл бұрын
kudos!!!!! very detail and beautifully explained.
@shivam9201
@shivam9201 4 жыл бұрын
superb explaination.. I was able to write the perfect code in an single attempt.. Thanks for the lecture
@niteshchaurasia2053
@niteshchaurasia2053 4 жыл бұрын
Can you share the code here? I am getting some error
@imPriyansh77
@imPriyansh77 Жыл бұрын
One more possible iterative way is possible : We find the minimum element using 'for' loop & swap it with the 0th index. And then call the sort() function on the array of size = n - 1, where n is the size of the original array
@RaviYadav-t6c1y
@RaviYadav-t6c1y 6 ай бұрын
JavaScriptCode For Recursive SortArray:- function sortArray(arr, s, end) { if (s >= end) { return arr; } let lastVal = arr.pop(); sortArray(arr, s, end - 1); insertVal(arr, lastVal); } function insertVal(arr, lastVal) { if (arr.length === 0 || arr[arr.length - 1]
@karanraoexperiment
@karanraoexperiment 2 жыл бұрын
Working Code: #include #include using namespace std; void insertTemp(vector&v, int temp){ //base condition if(v.size()==0 || v[v.size()-1]>n; vectorarr; for(int i=0;i>d; arr.push_back(d); } sortArray(arr); for(int i=0;i
@mohit84604
@mohit84604 2 жыл бұрын
javascript code :- function insert(array, temp) { if (array.length == 0) { array.push(temp) return } else if (array[array.length - 1]
@RiteshYadav-rc1np
@RiteshYadav-rc1np 3 жыл бұрын
oh man you are genius thanx for this awesome explanation
@Weekly_Coder
@Weekly_Coder 4 жыл бұрын
your videos are awesome..these videos help me to improve my knowledge of recursion ...please make a videos on backtracking also.
@viggicodes
@viggicodes Жыл бұрын
def insert(arr, temp): size = len(arr) # base - if arr is empty or we need to add in the end if size == 0 or arr[size -1 ] sorting the smaller array sort_arr(arr) # induction -> inserting the element in the right position insert(arr, temp)
@PatilRahul02
@PatilRahul02 13 күн бұрын
# python code for sort a stack using recursion def sort(arr): # base case if(len(arr) == 1): return arr # hypothesis lastElement = arr[len(arr)-1] arr.pop(-1) # induction sort(arr) insert(arr, lastElement) def insert(arr, temp): # base case if ( len(arr) == 0 or arr[len(arr) - 1]
@prakashhaldar2425
@prakashhaldar2425 3 жыл бұрын
You are jitu bhaiya 🙌🏻 of kota factory
@priyanshubharti5198
@priyanshubharti5198 4 жыл бұрын
The video explanation was very good🔥🔥
@shyampramanik780
@shyampramanik780 4 жыл бұрын
C# code: using System; using System.Linq; using System.Collections.Generic; class MainClass { public static void Main (string[] args) { var input = new List{11,6,15,8,10,2,9}; sort(input); foreach(var item in input){ Console.WriteLine(item); Console.WriteLine(" "); } } private static void sort(List input){ if(input.Count() == 1){ return; } var temp = input[input.Count() - 1]; input.RemoveAt(input.Count() - 1); sort(input); insert(input,temp); } private static void insert(List input, int element){ if(input.Count() == 0 || (input[input.Count() - 1]
@chetanraghavv
@chetanraghavv 10 ай бұрын
For anyone new here, if you don't want to solve the induction problem (inserting an element in a sorted array) using recursion, then you can use *Binary Search* to find its correct position in sorted array. This will boil down the time complexity from O(N^2) to O(NlogN) and space complexity from O(N^2) to O(N) (if we are considering recursive stack space in the solution, if we're not then it'll be O(1) for both cases)
@nikitajaiswal9112
@nikitajaiswal9112 2 жыл бұрын
6:18 pr aapne last wale element ko neglect kiya hum starting wale element ko bhi neglect kr skte the na?...
@shubhamgupta8723
@shubhamgupta8723 4 жыл бұрын
Thnx for making high quality content
@lomeshdaheria9960
@lomeshdaheria9960 4 жыл бұрын
Thanks 😊😊 bhai itna sahi se samjhane ke liye
@viditsinghbrahmania1476
@viditsinghbrahmania1476 4 жыл бұрын
Insert() wala part sahi se nhi samjhaya chief! Hypothesis likhne me dikkat ho rhi thi
@sarfarazalam6077
@sarfarazalam6077 3 жыл бұрын
Hypothesis : insert() will add the element in its correct position & resultant array will be sorted. One thing to observe that , insert() will always get sorted vector as its input.
@ChandanSingh-ku4eq
@ChandanSingh-ku4eq 3 ай бұрын
Java code: class Main { public static void main(String[] args) { List al= new ArrayList(Arrays.asList(2,3,7,6,4,5,9)); sort(al); System.out.println("Hello world! "+ al); } static void sort(List al){ if(al.size() ==1) return; Integer temp= al.get(al.size()-1); al.remove(al.size()-1); sort(al); insert(al, temp); return; } static void insert(List al, Integer temp) { if(al.size() == 0 || al.get(al.size()-1)
@gurupreetsingh8347
@gurupreetsingh8347 4 жыл бұрын
bhai aadi, yeh jo fabonaci series hai [ fabo(n-1) + fabo(n-2) ] yeh IBH ka pattern follow kar rahi hai ya fir choices + decision ,, mujhe todi confusion hai isse mein ,, waise mere khayal se yeh IBH hona chahiye, because iss mein choices hai hi nahi , right ??? aap thodi light daalo bhai issss pe ?????
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
Yaar basically dekho cheej ek hi hai sb, ye to mene bna diye hai based on my experience so that one could easily approach a problem. Talking about fibonacci its based on IBH as in hypothesis step we calculate n-1 and n-2 recursive calls and in induction we add the results from those calls !!
@gurupreetsingh8347
@gurupreetsingh8347 4 жыл бұрын
@@TheAdityaVerma ​ Aditya bas bhai yehi clerity chahiye thi aap se , bas samja gya , yaar ne jo bana hai apne exp se sahi bana hai , pahali baar toh recursion samj aaraha hai,,, bas jo beech beech mein doubt aate hia vo main aapse puchh ke clear out kar leta hoon, THANKS bhai,,, bas aap doubt clear kar te rahna ,, aur hum bas aise sikh te chale jaye gye aap se,,, ..... THANKS BRO:)
@sahilkukreja2717
@sahilkukreja2717 3 жыл бұрын
Hi Aditya, Hope you are doing well If possible can you make one playlist or detailed session for time and space complexity.
@sr_snehil
@sr_snehil 3 жыл бұрын
PYTHON CODE:- def sort(arr): #base case if len(arr)==1: return arr #hypothesis temp=arr.pop() arr= sort(arr) #induction arr=insert(arr,temp) return arr def insert(arr,temp): #base case if len(arr)==0 or temp>=arr[-1]: arr.append(temp) return arr #hypothesis n=arr.pop() arr=insert(arr,temp) #induction arr.append(n) return arr arr=[2,1,3,4,1,5] print(sort(arr))
@jesmigeorge4936
@jesmigeorge4936 2 жыл бұрын
👍
@gargprince1702
@gargprince1702 4 жыл бұрын
lghta h iss week my recursion series puree ho jayegyi pls bhaiya add some questions for us to solve in the end of video
@prashantranpura7412
@prashantranpura7412 4 жыл бұрын
Rock solid explanation
@jyotijangid5243
@jyotijangid5243 4 жыл бұрын
Please include TOH and if you can add TC of the explained code at the end of the video it will be great.
@azmirvali6205
@azmirvali6205 Жыл бұрын
When I see ur videos I will keep aside all my distraction calm that much intrest in ur vds
@SuperSandy-lw3sy
@SuperSandy-lw3sy Жыл бұрын
9:40 jab vector size 0 rahega tab bhi toh array/vector sort rahega toh sir if(V.size==0 || V.size==1) return Hoga na
@himanshunegi5228
@himanshunegi5228 4 жыл бұрын
bhaiya how stack is used in recursion vo toh btao
@kushalappaca5324
@kushalappaca5324 3 жыл бұрын
That's the most primitive way of understanding recursion bro...It can be used to solve only simple problems and for complex problems stack trace becomes difficult... Follow the method he is teaching... It should work fine... Just know how stack in recursion works... Don't use it to solve problems
@anmolacharya309
@anmolacharya309 7 ай бұрын
I am still having difficulty understanding this especially the insert part? can someone help me, what exactly is the temp variable being used ?
@Abhishek7Shah
@Abhishek7Shah 4 ай бұрын
it's array
@faizannasimhyder9011
@faizannasimhyder9011 2 жыл бұрын
Best part about your videos is that at least you try your best to explain...
@madhuryareddy3994
@madhuryareddy3994 8 ай бұрын
class Solution: def sortArray(self, nums: List[int]) -> List[int]: self.sort(nums) return nums def sort(self,nums): if len(nums)==1: return temp = nums.pop() self.sort(nums) self.insert(nums,temp) def insert(self,nums,temp): if len(nums)==0 or nums[-1]
@suar_pilla
@suar_pilla 11 ай бұрын
@TheAdityaVerma I understood the logic clearly in case when using C++. but for Java the arrays works differently, can you tell me how to come up with the hypothesis for that case?
@chetanraghavv
@chetanraghavv 10 ай бұрын
what'll be different in java? in java, it is not sure whether elements of array are stored adjacent to each other in memory but still they can be accessed in O(1) using index or the reference to that element. are you referring to this? I think hypothesis is language independent so it shouldn't be a concern if you're using Java. Hypothesis is basically what the function is expected to do and we have to write induction so that expectation becomes reality and function actually starts doing what it is expected to do.
@suar_pilla
@suar_pilla 10 ай бұрын
@@chetanraghavv Trying applying this hypothesis in java, you will understand what i mean... until and unless u try this approach in Java , you will not get want I meant..
@subhamchoudhary9123
@subhamchoudhary9123 3 жыл бұрын
for O(1) space refer this : void SWAP(int &a,int &b){ int temp = a; a = b; b = temp; } void f(vector&arr,int N,bool swap){ if(N==-1 || (N==0 && !swap)) return ; if(swap) { if(arr[N]>arr[N+1]){ SWAP(arr[N],arr[N+1]); f(arr,N-1,true); } return; } f(arr,N-1,false); f(arr,N-1,true); }
@sapnafulwani5823
@sapnafulwani5823 3 жыл бұрын
Love your content!!
@muskangupta5873
@muskangupta5873 3 жыл бұрын
Thank you bhaiya for making these videos
@SukhmanSinghSandhu
@SukhmanSinghSandhu Жыл бұрын
Very good explanation
@VarunVishwakarma1
@VarunVishwakarma1 3 жыл бұрын
@AdityaVerma, Tree and graph lessons would be much appreciated. Recursion se to pyar ho hi gaya hai. Tree aur Grpah se bhi karwa do boss.
@samirkhandekar4678
@samirkhandekar4678 10 ай бұрын
Thanks @Aditya Verma Java impl import java.util.ArrayList; import java.util.List; public class SortArray { void sort(List arr){ if(arr.size() == 1){ return; } int lastVal = arr.remove(arr.size()-1); sort(arr); insert(arr, lastVal); } void insert(List arr, int elem){ if(arr.isEmpty() || arr.get(arr.size()-1)
Reverse a Stack using Recursion
14:59
Aditya Verma
Рет қаралды 131 М.
Hypothesis-Induction-Base Condition
20:16
Aditya Verma
Рет қаралды 207 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
Kth Symbol in Grammar
23:32
Aditya Verma
Рет қаралды 128 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 344 М.
I Spent 100 Hours Inside The Pyramids!
21:43
MrBeast
Рет қаралды 66 МЛН
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
Sort A Stack
12:31
Aditya Verma
Рет қаралды 181 М.
Factor ANY Quadratic Equation Without Guessing | Outlier.org
14:02
Tower of Hanoi | Recursion
24:01
Aditya Verma
Рет қаралды 177 М.
Recursion for Beginners: A Beginner's Guide to Recursion
27:25
North Bay Python
Рет қаралды 109 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН