 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna do another lead code grind challenge lead code easy Sort array by Perry Given an array a of non negative integers return an array consisting of all the even elements of a followed by all the odd Elements of a you may return any answer array that's out of that satisfies the condition So Just given an array Return the even and then the odds Okay, so this is actually really easy and I'll just do it right now Basically, you want to go through your array once you want to ray called them Let's create an array called to return and then We're gonna go through the array I lost you. Well, there's really laggy a dot size so basically you just want to Go through it check if it's even go through all your values in your a if it's even and we're gonna add it to to return So if a at I Mod by to this how you check if even by the way Is equal to zero because mod means that you have a remainder, but if it's even then we're gonna we're gonna add it To our array We're turning then we then that's gonna end so that's that's gonna That basically adds up all the evens Into our array that we're returning then we're gonna go through it again. We're gonna go through the array that was Originally given to us and then we're gonna add all the odd values and that's basically how you Do this Problem there is a better way to do this using one pass and I'll go over that In a few minutes. Yeah, so if not equals zero that means it's odd then we're gonna add It so that this was gonna this is gonna add the odd values first exact first This is gonna add even values in the array that we're returning then it's gonna go through and gonna add the odd values Then we're gonna return To return so yeah, that's basically how you sort by parody. You just have an array of a integer non-negative integers go through it twice and Yeah This is basically how you do it Accept it. All right. Now. There's actually a better solution Where you don't go through it twice and you're actually going to just Change your original array a I'll show you it right now, but I'll show you in a few minutes But try to figure out how to do it how to do that solution first and I'll show it to you all right guys, so this is the Proper solution using one pass which when I say one pass I means only going through their way the array once and I don't I'm not like I'm not creating new array and adding values to it. I'm just gonna modify the original array and I'll return that so What do I mean by one in one pass? So yeah, so how would you how exactly would you go through it only once and then? Swap all the even values in the front and then all the odd values in the end Well, one way to do this is something called quick sort. Okay, I Know this quick sort is like. Oh my god quick sort a standard algorithm standard textbook algorithm But how how are we gonna use quick sort? Well? We are going to maintain two pointers one for the beginning and the end. I is the beginning J's end Basically, we are going to check Whatever If it's odd or even all right, so if the beginning is if the beginning is even and the end is odd right then Everything is correct Okay, like the beginning is even the end is odd everything is correct. So I'm gonna move my beginning Pointer where I'm at up by one and the end I'm at down by one. All right if The beginning is odd Whatever value I'm at the beginning and my end is even then I have to swap these values Okay, and then I would continue but if the Beginning is even and the end is odd. Oh The end is even the end is even then We know that the first position the beginning position is correct So we increment the beginning position and continue if the beginning is odd and the end is odd We know that and is correct and we decrease the end and continue So that way in all these four cases Whatever position where we're at like we're guaranteed that the beginning is Gonna be even the end is gonna be odd because we're gonna swap those values every time if the if it they're incorrect, but if one is correct and we move the Whatever position my beginning position is by one if the end is correct Then we decrease my ends position by one and we keep swapping until we get down To the case. So how do you code this? Well, I'll code it with you guys right now All right, we They use I and J but let's call it begin. So this is gonna be in the begin position Just to make sure make sure you guys understand it. We have a begin position and we have an end position and Position is gonna be eight out size minus one okay, whoops and We are going to while my begin position is less than the end position We are going to do this condition Okay, so if my begin Whatever my value at my begin position, right? If I'm a begin position mod to Okay, so this this basically checks if if it's Yeah, okay, so if it's greater than a at end position Mod to What does this do? This is checking that if my begin position is odd and my end position is even Right, do you guys get get it cuz if this was odd mod by two would return one and That's gonna be greater than This is gonna return zero right So if this is the the beginning is odd and the end is The end is even I'm gonna swap these values. Okay, so I'm gonna create a temporary array begin position Temporary variable and I'm gonna swap this is a code how to swap you create a temporary variable and then you set Uh See you create a temporary variable you set my whatever my begin position was originally I set my beginning to the end position my end to Begin position Begin position my bad temp temp temp, which was the begin position before it got modified. So yeah now we're gonna check If my begin position was even if my begin position was even that means it was around the right spot So then I'm going to increase my begin position That means it was right if my begin position was now if my end position was odd Right if my end position was odd, then it's in the right spot. So then I'm gonna decrease my end position So this guarantees that every time this condition happens my whatever. I'm swapping is always gonna be the begin in the first Begin is begins all will always be even Whenever I'm swapping and my end will always be odd Yeah, and then after this while loop we're gonna return Turn A. Yeah, that was that's basically it. Why is this faster than only 26%? It's strange What did I get originally My original solution is actually way a little faster than this that's strange Yeah, this was a this was a solution dates gave us so I just follow this Yeah, rate com subscribe. I'll check you guys later. Peace