Thread: Sorting an array: Even numbers followed by Odd numbers

  1. #1
    Registered User
    Join Date
    Apr 2011

    Sorting an array: Even numbers followed by Odd numbers

    I am new to sorting techniques, i need a help on sorting of an array by Even numbers followed by Odd numbers in the array.
    int arr[10] = {5,3,4,12,1,6,15,24,35,2};
    int size = 10;
    int End_Count = size-1;
    int Start_Count = 0;
    int sort_array[10];
    for(int temp = 0; temp<size; temp++)
    		sort_array[End_Count] = arr[temp];
    		sort_array[Start_Count] = arr[temp];
    after the complete execution of the above for loop sort_arry contains
    sort_array = {4,12,6,24,2,35,15,1,3,5}
    Thats what actually my sorting has to be.
    But my question is, how can i do this sorting without using another array?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Since you already have the odd and even numbers separated on sort_array[]. You can use qsort() or any sort algorithm that you want and just pass the odd and even section of the array and the length to the function.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Since you are only interested in a partition rather than a full sort, it may be more efficient to implement something like a typical partition algorithm for quicksort.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    You're not actually sorting here, just partitioning. You can partition in place by imagining your array is divided into 3 sections: Evens at the beginning, odds at the end, and unknown in the middle. Going through the middle section, if a number is even, add it to the end of the even section, if odd, add to the beginning of the odd section. This is actually quite similar to what you're already doing.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Apr 2011
    Hello Everybody,
    Thank you for your valuble replies.

    Yes, actually i do not want to sort them by either ascending order or descending order.
    I just want to partitioning the array.
    But i do not want to create one more array(sort_array) for that.
    How can i partition the arr[10] by even numbers followed by odd numbers.
    Note: arr size is static, not a dynamic.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    laserlight is correct, what you want to do is use something similar to the partitioning step from inside quicksort.

    Approximate pseudocode:
    start with left equals zero and right equals length
    while first is left than right
      while left is less than right, and left item is even, increment left
      decrement right, and repeat until left is not less than right, or left item is odd
      if left is not less than right then exit loop
      swap left item and right item
    end while
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array's Question sorting numbers!
    By Jomoka in forum C Programming
    Replies: 7
    Last Post: 06-16-2011, 10:19 AM
  2. Sorting the numbers
    By minyoungan in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 11:10 AM
  3. help with sorting numbers
    By artistunknown in forum C Programming
    Replies: 12
    Last Post: 02-07-2010, 12:00 AM
  4. Can anyone help me to break numbers into an array numbers
    By s2xcracker in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2009, 05:17 PM
  5. sorting an array of numbers in C
    By JVincent08 in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 01:42 PM