Computer Science, asked by Traitor, 1 year ago

how to swap even elements to the right side and odd elements to the left side in an 1D array


NikhilMTomy: What programming language are you using
Traitor: c++

Answers

Answered by NikhilMTomy
1
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int a[50],n,i;
     cout<<"Enter limit : ";
     cin>>n;
     cout<<"Enter elements of the array\n";
     for(i=0;i<n;i++)
         cin>>a[i];
     for(i=0;i<n;i++)
     {
         if(a[i]%2==0)
         {
             for(j=i+;j<n;j++)
             {
                   if(a[j]%2!=0)
                   {
                         temp=a[i];
                         a[i]=a[j];
                         a[j]=temp;
                   }
               }
         } 
    }
   cout<<"Array after arrangement \n";
   for(i=0;i<n;i++)
   cout<<a[i]<<endl;
   getch();
}


Traitor: thnx
Traitor: I don't think u need those many flower brackets i.e., {}
NikhilMTomy: It will increase readability and sense of direction.
Traitor: ok
Answered by arakkalvaishnav
1
{
int temp,a[20],b[20];
for(i=0,m=0;i<n;i++)
for(j=n;j>0;)
{
  if(a[i]%2==0)
   {
     b[m]==a[i];
     m++;
   }
  else
    {
      b[j]=a[i];
      j--;
   }
}}
     
i think this would work
Similar questions