# Sort all even numbers in ascending order and then sort all odd numbers in descending order

Given an array of integers (both odd and even), sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.

Examples:

```Input  : arr[] = {1, 2, 3, 5, 4, 7, 10}
Output : arr[] = {7, 5, 3, 1, 2, 4, 10}

Input  : arr[] = {0, 4, 5, 3, 7, 2, 1}
Output : arr[] = {7, 5, 3, 1, 0, 2, 4}
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Method 1 (Using Partition)

1. Partition the input array such that all odd elements are moved to left and all even elements on right. This step takes O(n).
2. Once the array is partitioned, sort left and right parts individually. This step takes O(n Log n).

Below is implementation of above idea.

## C++

```// C++ program sort array in even and odd manner.
// The odd numbers are to be sorted in descending
// order and the even numbers in ascending order
#include<bits/stdc++.h>
using namespace std;

// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
void twoWaySort(int arr[], int n)
{
// Current indexes from left and right
int l = 0, r = n-1;

// Count of odd numbers
int k = 0;

while (l < r)
{
// Find first odd number from left side.
while (arr[l]%2 != 0)
{
l++;
k++;
}

// Find first even number from right side.
while (arr[r]%2 == 0  && l<r)
r--;

// Swap odd number present on left and even
// number right.
if (l < r)
swap(arr[l], arr[r]);
}

// Sort odd number in descending order
sort(arr, arr+k, greater<int>());

// Sort even number in ascending order
sort(arr+k, arr+n);
}

// Driver code
int main()
{
int arr[] = {1, 3, 2, 7, 5, 4};
int n = sizeof(arr)/sizeof(int);
twoWaySort(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
```

## Java

```// Java program sort array in even and odd manner.
// The odd numbers are to be sorted in descending
// order and the even numbers in ascending order

import java.util.Arrays;
import java.util.Collections;

public class GFG
{
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
static void twoWaySort(Integer arr[], int n)
{
// Current indexes from left and right
int l = 0, r = n-1;

// Count of odd numbers
int k = 0;

while (l < r)
{
// Find first odd number from left side.
while (arr[l]%2 != 0)
{
l++;
k++;
}

// Find first even number from right side.
while (arr[r]%2 == 0  && l<r)
r--;

// Swap odd number present on left and even
// number right.
if (l < r)
{
//  swap arr[l] arr[r]
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;

}

}

// Sort odd number in descending order
Arrays.sort(arr, 0, k,Collections.reverseOrder());

// Sort even number in ascending order
Arrays.sort(arr, k, n);
}

// Driver Method
public static void main(String[] args)
{
Integer arr[] = {1, 3, 2, 7, 5, 4};

twoWaySort(arr, arr.length);

System.out.println(Arrays.toString(arr));
}
}
```

Output:

```7 5 3 1 2 4
```

Time complexity: O(n log n)
space complexity: O(1)

Method 2 (Using negative multiplication) :

1. Make all odd numbers negative.
2. Sort all numbers.
3. Revert the changes made in step 1 to get original elements back.

## C++

```// C++ program sort array in even and odd manner.
// The odd numbers are to be sorted in descending
// order and the even numbers in ascending order
#include<bits/stdc++.h>
using namespace std;

// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
void twoWaySort(int arr[], int n)
{
// Make all odd numbers negative
for (int i=0 ; i<n ; i++)
if (arr[i] & 1) // Check for odd
arr[i] *= -1;

// Sort all numbers
sort(arr, arr+n);

// Retaining original array
for (int i=0 ; i<n ; i++)
if (arr[i] & 1)
arr[i] *= -1;
}

// Driver code
int main()
{
int arr[] = {1, 3, 2, 7, 5, 4};
int n = sizeof(arr)/sizeof(int);
twoWaySort(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
```

## Java

```// Java program sort array in even and odd manner.
// The odd numbers are to be sorted in descending
// order and the even numbers in ascending order

import java.util.Arrays;

public class GFG
{
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
static void twoWaySort(int arr[], int n)
{
// Make all odd numbers negative
for (int i=0 ; i<n ; i++)
if ((arr[i] & 1) != 0) // Check for odd
arr[i] *= -1;

// Sort all numbers
Arrays.sort(arr);

// Retaining original array
for (int i=0 ; i<n ; i++)
if ((arr[i] & 1) != 0)
arr[i] *= -1;
}

// Driver Method
public static void main(String[] args)
{
int arr[] = {1, 3, 2, 7, 5, 4};

twoWaySort(arr, arr.length);

System.out.println(Arrays.toString(arr));
}
}
```

Output:

`7 5 3 1 2 4 `

Time complexity: O(n log n)
space complexity: O(1)

This method may not work when input array contains negative numbers. However, there is a way to handle this. We count the positive odd integers in the input array then sort again. Readers may refer this for implementation.

Thanks to Amandeep Singh for suggesting this solution.

This article is contributed by DANISH_RAZA. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.3 Average Difficulty : 2.3/5.0
Based on 18 vote(s)