Sorting Arrays - Even and Odd Input

This is a discussion on Sorting Arrays - Even and Odd Input within the C Programming forums, part of the General Programming Boards category; Originally posted by deedlit Is it possible to add something to this program to make it so the latter half, ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by deedlit
    Is it possible to add something to this program to make it so the latter half, the odd numbers, are also in the same order they began in at the end of the array?

    Ex:

    before: 1428754744
    after: 4284441757

    Thanks
    Yes it's possible.

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Since you're new to functions, a good example might be an exercise in code reuse. Here, we just use a generic 'copy_if' function to do all the work.


    Code:
    #ifndef BOOL
    typedef int BOOL;
    #endif
    
    int copy_if(
     int destination[], 
     int * dest_length, 
     int source[], 
     int src_length, 
     BOOL (*criteria)(int)
     )
    {
     int s, d = 0;
      
        for(s = 0; s < src_length; ++s)
       {
           if( criteria(source[s]) )
          {
            destination[d++] = source[s];
          }   
       }
     
     if(dest_length != NULL) *dest_length = d;
     
     return d;
    } 
    
     
    BOOL is_even(int number)
    {
     return number % 2 == 0;
    } 
    
    
    BOOL is_odd(int number)
    {
     return !is_even(number);
    }
    
    
    BOOL is_anything(int number)
    {
     return 1;
    }
    
    
    
    int main()
    {
     const int sz = 6;
     int array[sz] = {63, 12, 10, 11, 8, 77};
     int even[sz], ez;
     int odd[sz], oz;
     
     
     copy_if(even, &ez, array, sz, is_even);
     
     copy_if(odd, &oz, array, sz, is_odd);
     
     copy_if(array, NULL, even, ez, is_anything);
     
     /* we know &array[ez] is the next free space */
    
     copy_if(&array[ez], NULL, odd, oz, is_anything); 
     
     for(int i = 0; i < sz; ++i)
      printf("%d ", array[i]); 
     
     return 0;    
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #18
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by deedlit
    Is it possible to add something to this program to make it so the latter half, the odd numbers, are also in the same order they began in at the end of the array?

    Ex:

    before: 1428754744
    after: 4284441757

    Thanks
    Yes, it's very possible. Think about it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #19
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Thanks again.

  5. #20
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    correct me if I'm mistaken, but qsort isn't a stable sort, and [he|she]'s asking for a stable sort, so another sorting algorithm is required.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21