C program to segregate even and odd numbers

In this blog post, we learn how to write a C program to segregate even and odd numbers? So here we will write the C program to segregate even and odd numbers. We will also see how to segregate even and odd numbers for the given array.

So let’s see the logic to segregate even and odd numbers for the given array. Suppose arr is an integer array of size N (arr[N] ), the task is to write the C program to segregate even and odd numbers.

Examples,

Input  Array = {12, 34, 45, 9, 8, 90, 3};


Output Array = {12, 34, 8, 90, 45, 9, 3};

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

 

C program to segregate even and odd numbers:

 

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

// A Lomuto partition based scheme to segregate
// even and odd numbers.
void segregateEvenOdd(int arr[], int n)
{
    int j = -1;
    int i = 0;

    for (i = 0; i < n; i++)
    {
        // If array of element
        // is odd then swap
        if (arr[i] % 2 == 0)
        {
            // increment j by one
            j++;
            // swap the element
            swap(&arr[i], &arr[j]);
        }
    }
}


int main()
{
    int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };

    int i =0;

    int N = ARRAY_SIZE(arr);

    segregateEvenOdd(arr, N);

    for ( i = 0; i < N; i++)
    {
        printf("%d ",arr[i]);
    }

    return 0;
}

Output:

C program to segregate even and odd numbers

 

A second method to segregate even and odd numbers:

 

1. Create two variables left and right.

2. Initialize both variables left and right with  0 and n-1 (n is the size of the array).

3. Keep incrementing the left index until we see an odd number.

4. Keep decrementing the right index until we see an even number.

5. If left < right then swap arr[left] and arr[right].

 

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void segregateEvenOdd(int arr[], int n)
{
    int left = 0, right = n-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--;
        }
    }
}

int main()
{
    int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };

    int i =0;

    //Get the size of the array
    int N = ARRAY_SIZE(arr);

    segregateEvenOdd(arr, N);

    //print the array
    for (i = 0; i < N; i++)
    {
        printf("%d ",arr[i]);
    }

    return 0;
}

Output:

C program to segregate even and odd numbers

 

Recommended Articles for you: