Rohit's approach put into a typical c++ construct...

inline is_odd(int x)
{
  return ((x & 1) == 1);
}

struct new_compare {
   bool operator () (int i, int j) {
       bool b_i_odd = is_odd(i);
       bool b_j_odd = is_odd(j);

       if ((b_i_odd && b_j_odd) || (!b_i_odd && !b_j_odd))
            return (i < j);
       else if (b_i_odd && !b_j_odd)
            return 1;
       else
           return 0;
   }
};
std::vector<int> array_to_sort;
std::sort( array_to_sort.begin(), array_to_sort.end(), new_compare );

Of course, everything written above can be stated as a C program, or an
algorithm.

The sort( ) function can be replaced with any in-place sorting algorithm,
the new_compare class with either a function ptr, or a simple in-place
check.


On Tue, Jun 22, 2010 at 11:10 PM, Naveen Kumar
<[email protected]>wrote:

> Hi Rohit,
>
> Can you explain your approach a bit more?
>
> On Sun, Jun 20, 2010 at 2:48 PM, Rohit Saraf
> <[email protected]> wrote:
> > Why not just change the definition of when one number is bigger than
> another
> > and do normal sort ?
> > I guess that is better and simpler.
> > --------------------------------------------------
> > Rohit Saraf
> > Second Year Undergraduate,
> > Dept. of Computer Science and Engineering
> > IIT Bombay
> > http://www.cse.iitb.ac.in/~rohitfeb14
> >
> >
> > On Sun, Jun 20, 2010 at 7:52 AM, Anurag Sharma <[email protected]>
> > wrote:
> >>
> >> Keep 2 pointers 'start' and 'end' and make them point to start and
> >> beginning of the array.
> >>
> >> Now keep decresing end pointer until an odd element is found
> >> Keep increasing the start pointer until an even element is found
> >> swap the elements at start and end
> >> Continue the above 3 steps till start<end
> >>
> >> Now the start/end points to a border element which divides the array in
> 2
> >> parts, 1st have having all odd numbers and 2nd half with all even
> numbers.
> >>
> >> Now use any inplace sorting algorithm to sort in descending order the
> >> portion containing all odd numbers and in increasing order the portion
> >> containing all  even numbers.
> >> Hope its clear.
> >>
> >> Anurag Sharma
> >>
> >>
> >> On Sun, Jun 20, 2010 at 2:15 AM, vijay <[email protected]> wrote:
> >>>
> >>>  There is an array of odd and even numbers. Now, sort them in such a
> >>> way that the top portion of the array contains odd numbers, bottom
> >>> portion contains even numbers. The odd numbers are to be sorted in
> >>> descending order and the even numbers in ascending order. You are not
> >>> allowed to use any extra array and it has to use a conventional
> >>> sorting mechanism and should not do any pre or post processing
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> Groups
> >>> "Algorithm Geeks" group.
> >>> To post to this group, send email to [email protected].
> >>> To unsubscribe from this group, send email to
> >>> [email protected]<algogeeks%[email protected]>
> .
> >>> For more options, visit this group at
> >>> http://groups.google.com/group/algogeeks?hl=en.
> >>>
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "Algorithm Geeks" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected]<algogeeks%[email protected]>
> .
> >> For more options, visit this group at
> >> http://groups.google.com/group/algogeeks?hl=en.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<algogeeks%[email protected]>
> .
> > For more options, visit this group at
> > http://groups.google.com/group/algogeeks?hl=en.
> >
>
>
>
> --
> Cheers
> Naveen Kumar
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to