Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:03:03+00:00 2026-06-12T02:03:03+00:00

I am trying to solve a problem like … in an array we have

  • 0

I am trying to solve a problem like …
in an array we have to move all the odd elements to the start …and even elements to the end …
i tried this way but evens lose order here …can someone help me ???????
the output i get is …1 3 5 7 9 4 8 2 6
expecting an linear time in place solution …

 #include<stdio.h>
 void swap(int *p,int *q)
 {
 *p=*p^*q;
  *q=*p^*q;
  *p=*p^*q;
 }
  int main()
  {
  int arr[]={ 2, 1 ,4 ,3 ,6 ,5 ,8 ,7 ,9};
  int  odd=0;
  int even=0;
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  while(even< arr_size){
     if(arr[even]&1)
        swap(&arr[odd++],&arr[even++]);
     else
        even++;
  }
 int i=0;
 for(i=0;i<arr_size ;i++)
 printf("%d  ",arr[i]);
 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:03:05+00:00Added an answer on June 12, 2026 at 2:03 am

    How about this solution

    #include<stdio.h>
    
    
      int main()
      {
      int i;
      int arr[]={ 2, 1 ,4 ,3 ,6 ,5 ,8 ,7 ,9};
      int arr_size = sizeof(arr)/sizeof(arr[0]);
      int sorted[arr_size];
      int sp = 0;
      for(i=0;i<arr_size;i++){
         if(arr[i]&1){
            sorted[sp++]=arr[i];
         }
      }
    
      for(i=0;i<arr_size;i++){
         if(!(arr[i]&1)){
            sorted[sp++]=arr[i];
         }
      }
    
      for(i=0;i< arr_size ;i++)
        printf("%d  ", sorted[i]);
     return 0;
    }
    

    the output is

     1  3  5  7  9  2  4  6  8  
    

    ** UPDATE **

    while the above uses more space and runs over the list twice, the following runs over the list only once, but still use more space

     int main(){
    
      int i;
      int arr[]={ 2, 1 ,4 ,3 ,6 ,5 ,8 ,7 ,9};
      int arr_size = sizeof(arr)/sizeof(arr[0]);
      int sorted[arr_size];
      int even = 1+arr_size/2;
      int odd  = 0;
    
      for(i=0;i<arr_size;i++){
         if(arr[i]&1)
            sorted[odd++]=arr[i];
         else
            sorted[even++]=arr[i];
      }
    
      for(i=0;i< arr_size ;i++)
        printf("%d  ", sorted[i]);
    
     return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to solve this problem . I would like to learn how the bootstrapper
I'm trying to solve a problem which is something like this: I'm given n
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I am trying to solve this problem. I have a series of SELECT statements
I'm trying to solve a classic problem - I have a multi-threaded application which
I have a probably quite simple to solve problem. I'm trying to parse this
I've been trying to solve these problem for last two hours but seems like
Howdy all. I am trying to solve a problem which is apparently not uncommon
I've been trying to solve this problem. I would like to input a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.