In this lesson, we’ll practice problems that involve segregating (separating or dividing) the array based on different conditions.

Segregation using extra memory

For instance, given an array, D, we want to segregate the array based on the even and odd values of the array.

Segregating evens and odds

For this, we might want to find the even values from D and store these even values in a separate array, (Es). We can do the same for the odd values and store the odd values in a separate array, (Os). We can make a function that separates the values into even and odd values, (segregateEvensOdds()).

After we’ve separated both the even and odd values, we can replace the values of D with the even and odd values. We can make a separate function to replace the values of the array (replaceEsFollowedByOs()).

Look at the animation below to understand what is happening inside the segregateEvensOdds() function.

Get hands-on with 1200+ tech skills courses.