int j=1; for(i=2;i<n;i+=2) swap(a[i],a[j++]); if the input array is like: 2 3 4 5 6 7 8
then after this step u'll get: 2 4 6 8 3 7 5 now u can sort the two halves using any in-place sorting algo and u'll get: 8 6 4 2 3 5 7 i guess dis'll work... On Sat, May 28, 2011 at 12:26 AM, ross <[email protected]> wrote: > Hi all, > > Sort all elements in odd indices of an array in ascending order and > even indices in descending order. > Finally, rearrange so that all even indexed elements come first. > > eg: > > input – 7 2 6 4 8 3 1 > > even indexed : 7 6 8 1 => sort 8 7 6 1 > odd indexed: 2 4 3 => sort 2 3 4 > > output – 8 7 6 1 2 3 4 > > What could be the best algo to solve it? > Is it possible to arrive at the output using only O(1) extra space? > > -- > 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. > > -- regards Apoorve Mohan -- 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.
