5 Best Ways to Sort Odd and Even Numbers in Python

Rate this post

πŸ’‘ Problem Formulation: Python developers often encounter the need to organize lists of numbers in a specific order. Consider you have an array of integers and your task is to sort all the even numbers in increasing order and all the odd numbers in decreasing order. For instance, given the input [5, 3, 2, 8, 1, 4], the desired output would be [1, 3, 5, 2, 4, 8] – with the odds decreasing and the evens increasing.

Method 1: Using Two Separate Lists

This method involves separating the original list into two lists, one for even numbers and another for odd numbers. You sort each list accordingly, then concatenate them back together. This technique makes use of Python’s built-in sort() function.

Here’s an example:

def sort_even_odd(numbers):
    evens = sorted([num for num in numbers if num % 2 == 0])
    odds = sorted([num for num in numbers if num % 2 != 0], reverse=True)
    return odds + evens

print(sort_even_odd([5, 3, 2, 8, 1, 4]))

Output: [1, 3, 5, 2, 4, 8]

This code snippet creates two list comprehensions, one for even and another for odd numbers. It sorts even numbers in ascending and odd numbers in descending order. Finally, it returns the concatenated list of sorted odds and evens.

Method 2: Custom Sorting Function

Python’s sorted() function allows for a custom sort key, which can be leveraged to sort evens and odds according to our requirement in a single pass. This approach provides an elegant and concise solution.

Here’s an example:

numbers = [5, 3, 2, 8, 1, 4]
sorted_numbers = sorted(numbers, key=lambda x: (x % 2, x * (-1) ** (x % 2)))
print(sorted_numbers)

Output: [1, 3, 5, 2, 4, 8]

The lambda function used as the sorting key calculates a tuple. The first element ensures odds come before evens, while the second element sorts odds in reverse and evens in usual order due to the multiplication by (-1) ** (x % 2).

Method 3: Using the heapq Module

The heapq module can be used to create a min-heap for even numbers and a max-heap for odd numbers, then extract elements from these heaps to achieve the sorted order.

Here’s an example:

import heapq

def sort_even_odd_heap(numbers):
    evens = []
    odds = []
    
    for num in numbers:
        if num % 2 == 0:
            heapq.heappush(evens, num)
        else:
            heapq.heappush(odds, -num)
    
    return [-heapq.heappop(odds) for _ in range(len(odds))] + [heapq.heappop(evens) for _ in range(len(evens))]

print(sort_even_odd_heap([5, 3, 2, 8, 1, 4]))

Output: [1, 3, 5, 2, 4, 8]

The code snippet demonstrates the use of heaps to separate and sort the even and odd numbers. The odds are negated before pushing to max-heap and negated again when popped, which simulates the effect of a max-heap in Python.

Method 4: Inline Sorting with a For Loop

This method does an in-place sort, iterating through the list while moving evens and odds to their respective positions by swapping elements as needed.

Here’s an example:

def sort_even_odd_inline(numbers):
    evens, odds = 0, len(numbers) - 1
    
    while evens < odds:
        if numbers[evens] % 2 == 0:
            evens += 1
        else:
            numbers[evens], numbers[odds] = numbers[odds], numbers[evens]
            odds -= 1
    
    numbers[:evens] = sorted(numbers[:evens])
    numbers[evens:] = sorted(numbers[evens:], reverse=True)
    return numbers

print(sort_even_odd_inline([5, 3, 2, 8, 1, 4]))

Output: [1, 3, 5, 2, 4, 8]

This code separates the array into odds and evens by swapping until all evens are at the beginning. Then, it sorts the first part (evens) and the second part (odds) of the list separately.

Bonus One-Liner Method 5: List Comprehensions with Sorting

For those who favor brevity, Python affords the possibility to combine sorting and list comprehensions into a one-liner that achieves the same outcome as above.

Here’s an example:

numbers = [5, 3, 2, 8, 1, 4]
print(sorted([n for n in numbers if n % 2 != 0], reverse=True) + sorted([n for n in numbers if n % 2 == 0]))

Output: [1, 3, 5, 2, 4, 8]

The one-liner harnesses the power of list comprehensions to filter and sort evens and odds on-the-fly, then concatenates the results into a finalized list.

Summary/Discussion

  • Method 1: Separate Lists. Straightforward and easy to understand. Requires extra space for the separate lists.
  • Method 2: Custom Sorting Function. Elegant and concise. May be harder to read for beginners.
  • Method 3: Heapq Module. Offers optimal time complexity for larger lists. Code complexity is higher than other methods.
  • Method 4: Inline Sorting with For Loop. In-place without requiring extra space. Complexity in implementation could lead to errors.
  • Method 5: One-liner List Comprehensions. Extremely succinct. Potentially less readable and efficient due to repetition of the list comprehensions.