# Sorting an array: Even numbers followed by Odd numbers

• 01-09-2012
ArunS
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?
• 01-09-2012
mnd22
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.
• 01-09-2012
laserlight
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.
• 01-09-2012
NeonBlack
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.
• 01-09-2012
ArunS
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.
• 01-10-2012
iMalc
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```