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} 

Asked in : Microsoft

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 C++ implementation of above idea.

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

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++ 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 

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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.