### Ordering an array into ascending evens/descending odds

So I'm not very proud to ask a question without using any code,but it has to be done.

So I would like to sort an array into ascending evens and descending odds.

For exemple,if I have initialize an array

` myArray={0,1,2,3,4,5,6,7,8,9} `
,
after the sorting,myArray would be:

` myArray={0,2,4,6,8,9,7,5,3,1} `

I was thinking maybe I could split my array into odds and even using some sort of Divide et Impera (the problem is that I dont know if it's possible,and if it is,well...I cant figure out the algorithm),and then QuickSort the odds and the evens..

Can you halp,cplusplus.com members?
Last edited on
I would use std::sort with a custom comparator:
http://www.cplusplus.com/reference/algorithm/sort/
Let me know if you can't figure it out, but all the comparator has to do is make evens come before odds and make evens sort normally and odds sort in reverse. It will be one boolean expression ;)
I cant use std::sort..
It's pretty much a homework and well..I'm supposed to learn the algorithm logic,not the syntax,I guess.
Do you know how to write your own sorting algorithm? Start with that first and then change it to sort like this instead.
Last edited on
I was looking for an efficient way,not for just a way.
I don't know what you want me to tell you. If you cannot use std::sort and you find yourself incapable of writing a sorting algorithm that you consider efficient enough for your desires, what do you expect me to say?
It is the efficient way.

The OP mentions QuickSort with implication that the student (1) can write it and (2) considers it to be efficient.

What L B did suggest was that you write that QuickSort. In the algorithm you will compare the elements. You will probably write `(a < b)`. Replace that expression with the comparator mentioned by L B.

 I cannot remember what tricks QuickSort uses, so maybe it isn't the optimal, but since OP must have studied variaous sorting algorithms, he knows the features of the alternatives.
Last edited on
I realize the topic I started is pretty vague.

I'm sorry for crowding up the forum,I really am..

I can write a QuickSort,I just wasn't sure how do the odd/even splitting thing,that's what I was searching for.

Sorry for bothering, cplusplus.com.
 I just wasn't sure how do the odd/even splitting thing

Are you now sure, i.e. how do you see the L B's suggestion now?
No,I'm not,but I haven't got the time to code until now. I will probably figure it out.

I respect everyone's suggestions equally. Just the fact that they took the time to write it down it's something I respect.

I'll start coding now and post what I come up with to see if you have any suggestions on improving my code.
Last edited on
OK. This is what I came up with:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` `````` #include using namespace std; int * myArray; int n; int x; bool ascending(int left,int right){ return leftright; } int pivot(int myArray[],int left,int right,bool (*comparision)(int,int)); int firstSort(int myArray[],int left,int right); void quickSort(int myArray[],int left,int right,bool (*comparision)(int,int)); void init(); int main(){ init(); x=firstSort(myArray,0,n-1); quickSort(myArray,0,x-1,ascending); quickSort(myArray,x,n-1,descending); for(int i=0;i

What do you think?
Topic archived. No new replies allowed.