Python | Rearrange Positive and Negative Elements
Last Updated :
05 Apr, 2023
Sometimes, while working with Python lists, we can have a problem in which we need to perform a sort in list. There are many variations of sorting that might be required to perform. Once such variation can be to sort elements of list, but keeping in mind, positive elements appear before negative elements. Let’s discuss a way in which this task can be performed.
Method 1: Using sorted() + lambda
This task can be performed using a combination of the above functions. In this, the logic we apply is computing the inverse and negative of each number and then performing the sort using sorted(). The inversion makes sure that larger magnitude elements occur in the middle and smaller at ends ( hill arrangement ) and the negative takes care of making positives occur before negatives.
Python3
test_list = [ 4 , - 8 , - 6 , 3 , - 5 , 8 , 10 , 5 , - 19 ]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda i: 0 if i = = 0 else - 1 / i)
print ( "Result after performing sort operation : " + str (res))
|
Output
The original list is : [4, -8, -6, 3, -5, 8, 10, 5, -19]
Result after performing sort operation : [3, 4, 5, 8, 10, -19, -8, -6, -5]
Time complexity: O(nlog(n))
This is because the sorting algorithm used is QuickSort, which has a time complexity of O(nlog(n)).
Auxiliary Space: O(n)
This is because we are creating a new list of the same size as the original list.
Method 2: Using sort() and extend() methods
Python3
test_list = [ 4 , - 8 , - 6 , 3 , - 5 , 8 , 10 , 5 , - 19 ]
print ( "The original list is : " + str (test_list))
res1 = []
res2 = []
for i in test_list:
if (i > 0 ):
res1.append(i)
else :
res2.append(i)
res1.sort()
res2.sort()
res1.extend(res2)
print ( "Result after performing sort operation : " + str (res1))
|
Output
The original list is : [4, -8, -6, 3, -5, 8, 10, 5, -19]
Result after performing sort operation : [3, 4, 5, 8, 10, -19, -8, -6, -5]
Time complexity: O(n log n)
since it uses a sorting algorithm
Auxiliary Space: O(n)
since it uses an additional list to store the sorted elements
Method 3: Using the built-in filter() function along with a lambda function
Using the built-in filter() function along with a lambda function to separate the positive and negative elements in the list, and then using the built-in sort() function to sort the two lists separately. Then use the built-in extend() function to merge the two lists together.
Python3
test_list = [ 4 , - 8 , - 6 , 3 , - 5 , 8 , 10 , 5 , - 19 ]
print ( "The original list is : " + str (test_list))
res = sorted ( filter ( lambda x: x > 0 , test_list)) + \
sorted ( filter ( lambda x: x < 0 , test_list))
print ( "Result after performing sort operation : " + str (res))
|
Output
The original list is : [4, -8, -6, 3, -5, 8, 10, 5, -19]
Result after performing sort operation : [3, 4, 5, 8, 10, -19, -8, -6, -5]
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Method 4: Using List comprehension
Using List comprehension, sort positive and negative separately and then concatenate the lists.
Python
test_list = [ 4 , - 8 , - 6 , 3 , - 5 , 8 , 10 , 5 , - 19 ]
print ( "The original list is : " + str (test_list))
positive_nums = [x for x in test_list if x > = 0 ]
negative_nums = [x for x in test_list if x < 0 ]
positive_nums.sort()
negative_nums.sort()
res = positive_nums + negative_nums
print ( "Result after performing sort operation : " + str (res))
|
Output
The original list is : [4, -8, -6, 3, -5, 8, 10, 5, -19]
Result after performing sort operation : [3, 4, 5, 8, 10, -19, -8, -6, -5]
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Method 5: Using two pointers and swapping
Approach:
- Initialize two pointers, left pointing to the first element of the list and right pointing to the last element of the list.
- Loop until left pointer crosses the right pointer:
a. Check if the left element is negative and right element is positive, swap them.
b. If the left element is positive, move the left pointer to the right.
c. If the right element is negative, move the right pointer to the left.
- The list is sorted with positive numbers before negative numbers
Python3
test_list = [ 4 , - 8 , - 6 , 3 , - 5 , 8 , 10 , 5 , - 19 ]
print ( "The original list is : " + str (test_list))
left, right = 0 , len (test_list) - 1
while left < = right:
if test_list[left] < 0 and test_list[right] > = 0 :
test_list[left], test_list[right] = test_list[right], test_list[left]
if test_list[left] > = 0 :
left + = 1
if test_list[right] < 0 :
right - = 1
print ( "Result after performing sort operation : " + str (test_list))
|
Output
The original list is : [4, -8, -6, 3, -5, 8, 10, 5, -19]
Result after performing sort operation : [4, 5, 10, 3, 8, -5, -6, -8, -19]
Time complexity: O(n)
where n is the length of the list.
Auxiliary space: O(1)
As we are not using any extra space to store the elements.
Method 6: Using itertools:
Algorithm:
- Initialize the list test_list
- Print the original list
- Create two sublists, one containing the positive elements and one containing the negative elements, using the filter function
- Sort the positive elements sublist in ascending order, using the sorted function
- Concatenate the two sublists using the + operator, with the positive elements sublist first
- Convert the resulting iterator into a list using the list function
- Print the sorted list
Python3
import itertools
test_list = [ 4 , - 8 , - 6 , 3 , - 5 , 8 , 10 , 5 , - 19 ]
print ( "The original list is : " + str (test_list))
res = list (itertools.chain(
sorted ( filter ( lambda x: x > 0 , test_list)),
itertools.filterfalse( lambda x: x > 0 , test_list)))
print ( "Result after performing sort operation : " + str (res))
|
Output
The original list is : [4, -8, -6, 3, -5, 8, 10, 5, -19]
Result after performing sort operation : [3, 4, 5, 8, 10, -8, -6, -5, -19]
Time Complexity: O(n log n)
The time complexity of this code is O(n log n), where n is the length of the input list. This is because sorting the positive elements takes O(k log k) time, where k is the number of positive elements, and sorting the negative elements also takes O(m log m) time, where m is the number of negative elements. Since k + m = n, the overall time complexity is O(n log n).
Auxiliary Space: O(N)
The space complexity of this code is O(n), where n is the length of the input list. This is because the code creates two sublists to store the positive and negative elements separately, each with at most n elements. The space complexity does not depend on the values of the elements or their order. Additionally, the sorted function creates a new sorted list, which also takes up O(n) space. Finally, the + operator creates a new concatenated list, which again takes up O(n) space. Thus, the total space complexity is O(n).
Share your thoughts in the comments
Please Login to comment...