re arrange index of numbers

This is a discussion on re arrange index of numbers within the C Programming forums, part of the General Programming Boards category; Hey guys just wondering how i would have an array of integers eg 12 8 4 5 14 44 7 ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    re arrange index of numbers

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    One way to look at it is that you are simply sorting the array, but with your own comparison requirements.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    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.....

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Another one could be.
    take two pointers.first to the first element and second to the last.
    Code:
    while(first<=second)
       {       while(*first is odd)       
                  { increment first;} 
             while(*second is even)
                   {decrement second;}
              if (first<second)
                {  swap elements}
           }
    this will do in O(n).
    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"

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Both sunny's and cbastard's algorithms do not solve the problem because order is not preserved.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  7. #7
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by Rashakil Fol
    Both sunny's and cbastard's algorithms do not solve the problem because order is not preserved.
    Order was never asked by bazzano...he simply wanted to seperate odd and even numbers

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by quzah
    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.
    I didn't knew that


    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

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 11:53 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21