Like Tree1Likes
  • 1 Post By laserlight

Sorting an array: Even numbers followed by Odd numbers

This is a discussion on Sorting an array: Even numbers followed by Odd numbers within the C Programming forums, part of the General Programming Boards category; I am new to sorting techniques, i need a help on sorting of an array by Even numbers followed by ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    66

    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.
    Code:
    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++)
    {
    	if(arr[temp]%2)
    	{
    		sort_array[End_Count] = arr[temp];
    		End_Count--;
    	}
    	else
    	{
    		sort_array[Start_Count] = arr[temp];
    		Start_Count++;
    	}
    }
    after the complete execution of the above for loop sort_arry contains
    Code:
    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
    Posts
    52
    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
    Location
    Singapore
    Posts
    22,313
    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.
    iMalc likes this.
    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

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    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
    Location
    dust
    Posts
    66
    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
    Location
    New Zealand
    Posts
    6,308
    laserlight is correct, what you want to do is use something similar to the partitioning step from inside quicksort.

    Approximate pseudocode:
    Code:
    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, 11:19 AM
  2. Sorting the numbers
    By minyoungan in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 12:10 PM
  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, 06:17 PM
  5. sorting an array of numbers in C
    By JVincent08 in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 02:42 PM

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