Hey guys just wondering how i would have an array of integers
eg
12 8 4 5 14 44 7 3 10
and rearrange it so all then odd numbers are shifted to the front
of the array and the even numbers are shifted to the end.
eg
5 7 3 12 8 4 14 44 10
Hey guys just wondering how i would have an array of integers
eg
12 8 4 5 14 44 7 3 10
and rearrange it so all then odd numbers are shifted to the front
of the array and the even numbers are shifted to the end.
eg
5 7 3 12 8 4 14 44 10
One way to look at it is that you are simply sorting the array, but with your own comparison requirements.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
pick up first element...if its odd then leave it...move forward...pick 2nd element...if its even then search(ahead) for the first odd number and exchange...then pick the next element...if its odd leave it....pick up next element and go on......
You'll get odd numbers in front and even numbers in the end.....
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
Another one could be.
take two pointers.first to the first element and second to the last.
this will do in O(n).Code:while(first<=second) { while(*first is odd) { increment first;} while(*second is even) {decrement second;} if (first<second) { swap elements} }
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
Both sunny's and cbastard's algorithms do not solve the problem because order is not preserved.
Do it the easy way:
1) Copy each odd number as you come across it, into a new array. Then move to step 2.
2) Copy each even number as you come across it into the new array. Then move to step 3.
3) Copy the new array over top of the first one.
Quzah.
Hope is the first step on the road to disappointment.
Order was never asked by bazzano...he simply wanted to seperate odd and even numbersOriginally Posted by Rashakil Fol
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
The word shifted implies keeping the order. When you shift bits, you aren't reordering them. You're sliding them down one direction or another.
Quzah.
Hope is the first step on the road to disappointment.
I didn't knew thatOriginally Posted by quzah
and bazzono gave this example
12 8 4 5 14 44 7 3 10
to
5 7 3 12 8 4 14 44 10
didn't ask for order
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
I did'nt bother about the order.
There are two ways of doing thing's.
1. brute force
2.optimal soln (or rather close to optimal because there will
always be another better soln).
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"