Thread: A question about sorting..

  1. #1
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    A question about sorting..

    Hello all,
    While I am preparing my test, I fould a question on a website:

    How to rearrange an array of elements that either all the integers in even-numbered positions will be even or all the integers in odd-numbered positions will be odd (not necessary to establish both).

    could anyone give me a simple C-like function? Thx a lot!

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    This is a start:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define ARR_SIZE 100  /* allow some extra slop */
    #define WORK_SIZE 75  /* in case we have more even than odd or vice-versa */
    
    
    int main(int argc, char *argv[]){        
    	char *result[5]={"even","odd"};
            int odd[WORK_SIZE]={0},
                even[WORK_SIZE]={0},
                big_array[ARR_SIZE]={0};
            int i=0,
                tmp=0;
            int odd_cnt=0,
                even_cnt=0,
                big_cnt=0;
            for(i=0;i<WORK_SIZE;i++) {
                     tmp=rand();
                     if(tmp%2) 
                          odd[odd_cnt++]=tmp;
                     else
                          even[even_cnt++]=tmp;                        
            }               
            for(i=0,even_cnt=0,odd_cnt=0,big_cnt=0;odd[odd_cnt]||even[even_cnt];i++,big_cnt++){                                 
                 if(big_cnt%2){
                 	    if(odd[odd_cnt])
                           big_array[i]=odd[odd_cnt++];                              
                 }else{
                        if(even[even_cnt])
                           big_array[i]=even[even_cnt++];                       
                 }
            }
            tmp=0;
            printf("Note: a value of zero is displayed as even, but is a filler\n");
            printf("      and so can be in either an odd or an even element, regardless.\n");
            for(i=0;i<=big_cnt;i++){
                  printf("element number %2d, element value %6d %-5s\n",
                            i,big_array[i],result[big_array[i]%2]);
            }
            return 0;
    }

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    if(tmp%2)
    Code:
    if(big_cnt%2){
    I would do this for speed and for fun
    Code:
    if(tmp&1==0) /*test if number is even)
    and
    Code:
    if(big_cnt&1==0) /*test if number is even)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Sorting Question
    By Rkukulski in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 01:37 PM
  3. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 10:21 PM