Yes, the algorithm works for negative numbers since the modulo operation handles both positive and negative integers.
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
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.
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_list
and odd_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_list
and odd_list
lists will now contain the even and odd numbers from the original list, respectively.
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)
Lines 1–14: The function sort_even_odd
is created with one parameter, input_list
, representing the list of numbers to be sorted. Two empty lists, even_list
and odd_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.
The provided solution has a time complexity of
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:
Haven’t found what you were looking for? Contact Us
Free Resources