Yes, the algorithm works for negative numbers since the modulo operation handles both positive and negative integers.
How to sort even and odd numbers in Python
Key takeaways:
Purpose of sorting even and odd numbers: This approach categorizes numbers into separate lists of even and odd values, making it easier to process or analyze these subsets individually.
Python implementation: The implementation demonstrates Python's ability to handle tasks like list manipulation and sorting efficiently using built-in methods like
.append()and.sort().Handling of negative numbers: The solution handles both negative and positive integers correctly, ensuring that the parity of the number determines whether it goes into the even or odd list.
Sorting a list of numbers is a common task in programming, and Python provides powerful tools for achieving this efficiently. When dealing with a list that contains both odd and even numbers, you can sort them into two separate lists based on their
Problem statement
Write a program that takes a list of integers as input, separates the even and odd numbers into two separate lists, sorts each list in ascending order, and then returns the sorted lists.
Solution to sort even and odd numbers
Below is the algorithmic strategy for categorizing a list of numbers in Python into separate sets of odd and even elements:
Create two empty lists,
even_listandodd_list, one for even numbers and another for odd numbers.Iterate through each element in the input list.
Check the parity of the current number (whether it's even or odd).
Append the number to the appropriate list based on its parity.
After iterating through all elements, sort both even and odd lists separately.
The sorted
even_listandodd_listlists will now contain the even and odd numbers from the original list, respectively.
Python code to sort even and odd numbers
Let’s look at the code of the above solution:
def sort_even_odd(input_list):even_list = []odd_list = []for num in input_list:if num % 2 == 0:even_list.append(num)else:odd_list.append(num)even_list.sort()odd_list.sort()return even_list, odd_list# Example usageinput_numbers = [9, 2, 7, 4, 5, 8, 3, 6, 1]sorted_even, sorted_odd = sort_even_odd(input_numbers)print("Original list:", input_numbers)print("Sorted even numbers:", sorted_even)print("Sorted odd numbers:", sorted_odd)
Code explanation
Lines 1–14: The function
sort_even_oddis created with one parameter,input_list, representing the list of numbers to be sorted. Two empty lists,even_listandodd_list, are initialized to store even and odd numbers separately.Lines 5–9: We use a for loop to separate the even or odd numbers into their respective lists.
Lines 11–12: After categorizing odd and even numbers into their respective lists, we can proceed to independently sort each list.
Time complexity
The provided solution has a time complexity of
Space complexity
The space complexity is even_list and odd_list, each store a portion of the input numbers. As the input size increases, the space required for these lists also increases proportionally.
To further strengthen your coding skills and boost your technical interview preparation, here are some LeetCode problems to practice:
Frequently asked questions
Haven’t found what you were looking for? Contact Us
Can the algorithm handle negative numbers?
Does the function modify the original input list?
Can the function be extended to sort numbers in descending order?
What if all numbers are either even or odd?
Free Resources