Sort even number in ascending order and odd number in descending order C++

Given: An array of integers containing even and odd numbers.

Problem: For the given array sort even number in ascending order and then sort all the odd numbers in descending order. Then arrange odd numbers first and then arrange the odd numbers.

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}

Solution 1: Using Partition.

  • First move all the odd number to left of the array and even number to the right of given array.
  • Then sort the odd numbers (left side) in descending order and sort the even numbers (right side) in descending order.
  • Then the resulted array is the required output.

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

C++ Program:

// 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 even number 
        // from left side. 
        while (arr[l] % 2 != 0) 
        { 
            l++; 
            k++; 
        } 

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

        // Swap even number present on left and odd 
        // 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; 
} 

Output:

7 5 3 1 2 4

Solution 2: Using negative multiplication

  • Multiply odd numbers with -1 and make them negative.
  • Sort all the numbers in the array.
  • Again multiply the odd numbers with -1 to make them positive again.

Time complexity: O(n log n)
Space complexity: O(n)

C++ Program:

// 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; 
}

Output:

7 5 3 1 2 4

You can share your views in comments if you have any query, suggestion regarding sort even number in ascending order and odd number in descending order/

Leave a Comment