Program to separate even and odd numbers from an array

bookmark

#include<iostream>
using namespace std;

void swap(int *x, int *y)
{
  int t = *x;
  *x = *y;
  *y = t;
}

void segregate(int array[], int n)
{
  int left = 0, right = n - 1;
  while (left < right)
  {
    while (array[left] % 2 == 0 && left < right)
      left++;
    while (array[right] % 2 == 1 && left < right)
      right--;
    if (left < right)
    {
      swap(&array[left], &array[right]);
      left++;
      right--;
    }
  }
}

int main()
{
  int array[100], n, i;
  cout << "Enter number of elements: ";
  cin >> n;
  cout << "\nEnter elements: ";

  for (i = 0; i < n; i++)
    cin >> array[i];

  cout << "Original array: ";
  for (int i = 0; i < n; i++)
    cout << array[i] << " ";

  segregate(array, n);
  cout << "\nArray after divided: ";

  for (int i = 0; i < n; i++)
    cout << array[i] << " ";

  return 0;
}

 

Output

Enter number of elements: 5

Enter elements: 1 2 3 4 5
Original array: 1 2 3 4 5
Array after divided: 2 4 1 3 5