Thread: Sorting Arrays - Even and Odd Input

  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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.

Popular pages Recent additions subscribe to a feed