Wednesday, July 3, 2024
HomeLanguagesPythonPython List Comprehension | Sort even-placed elements in increasing and odd-placed in...

Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order

We are given an array of n distinct numbers, the task is to sort all even-placed numbers in increasing and odd-place numbers in decreasing order. The modified array should contain all sorted even-placed numbers followed by reverse sorted odd-placed numbers. Note that the first element is considered as even because of its index 0. Examples:

Input:  arr[] = {0, 1, 2, 3, 4, 5, 6, 7}
Output: arr[] = {0, 2, 4, 6, 7, 5, 3, 1}
Even-place elements : 0, 2, 4, 6
Odd-place elements : 1, 3, 5, 7
Even-place elements in increasing order : 
0, 2, 4, 6
Odd-Place elements in decreasing order : 
7, 5, 3, 1

Input: arr[] = {3, 1, 2, 4, 5, 9, 13, 14, 12}
Output: {2, 3, 5, 12, 13, 14, 9, 4, 1}
Even-place elements : 3, 2, 5, 13, 12
Odd-place elements : 1, 4, 9, 14
Even-place elements in increasing order : 
2, 3, 5, 12, 13
Odd-Place elements in decreasing order : 
14, 9, 4, 1

We have existing solution for this problem please refer Sort even-placed elements in increasing and odd-placed in decreasing order link. We can solve this problem in python quickly using List Comprehension. Approach is very simple,

  1. Separate original list into two parts, one contains all even indexed elements and one contains all odd indexed elements.
  2. Now sort list containing all even indexed elements in ascending order and list containing all odd indexed elements in descending order. Now concatenate both of them.

Python3




# Function to Sort even-placed elements
# in increasing and odd-placed in decreasing
# order
 
def evenOddSort(input):
 
     # separate even odd indexed elements list
     evens = [ input[i] for i in range(0,len(input)) if i%2==0 ]
     odds = [ input[i] for i in range(0,len(input)) if i%2!=0 ]
 
     # sort evens in ascending and odds in
     # descending using sorted() method
     print (sorted(evens) + sorted(odds,reverse=True))
 
# Driver program
if __name__ == "__main__":
    input = [0, 1, 2, 3, 4, 5, 6, 7]
    evenOddSort(input)


Output:

[0, 2, 4, 6, 7, 5, 3, 1]

Time complexity: O(n log n)

Auxiliary space: O(n)

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments