Using a while loop write pseudocode to fill an array called
Using a while loop, write pseudocode to fill an array called odds with all the odd numbers from 1 to 19. In other words, say the array called odds starts off as just being this: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]. It has to end up being this: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19].
Solution
void segregateEvenOdd(int arr[], int size)
{
/* Initialize left and right indexes */
int left = 0, right = size-1;
while (left < right)
{
/* Increment left index while we see 0 at left */
while (arr[left]%2 == 0 && left < right)
left++;
/* Decrement right index while we see 1 at right */
while (arr[right]%2 == 1 && left < right)
right--;
if (left < right)
{
/* Swap arr[left] and arr[right]*/
swap(&arr[left], &arr[right]);
left++;
right--;
}
}
}
