O(n) solution:

odd_index=-1;
even_index=-1;

//now find first odd no.'s index
for(i=0;i<n;i++)
    if(a[i]%2!=0)
    {
        odd_index=i;
        break;
    }

//if odd_index==-1 return; (only even no.s are there)

//select any even no.'s index after odd_index and swap

for(i=odd_index+1;i<n;i++)
{
    if(a[i]%2 == 0)
    {
        even_index = i;
        swap(a[odd_index],a[even_index]);
        odd_index++;    //no. next to a[odd_index] will always be odd after
swapping
    }
}

complexity:O(n)+O(n)=O(n);

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to