Separate even and odd number in same order

Question - 
Given an array A[], write a function that segregates even and odd numbers. The functions should put all even numbers first, and then odd numbers.
Note - ordre doesn't matter.
Example - 
Input :arr[] = {1, 2, 3, 4, 5, 6}
Output : {2, 4, 6, 1, 3, 5}

Approach -
First we print even elements and then print odd elements using two for loop.

Implementation - 

#include<iostream>
using namespace std;
void separate(int arr[],int n)
{
    int i;
    for(i=0;i<n;i++)
        if(arr[i]%2==0)
        cout<<arr[i]<<" ";
    for(int j=0;j<n;j++)
        if(arr[j]%2!=0)
        cout<<arr[j]<<" ";
}
int main()
{
    int n;
    cout<<"Enter the number of the elements : ";cin>>n;
    int arr[n];
    cout<<"Enter elements : "<<endl;
    for(int i=0;i<n;i++)
        cin>>arr[i];

    separate(arr,n);
    return 0;

}

Time complexity - O(n)

Comments