Sort Even-Placed in Increasing and Odd-placed Elements in Decreasing Order - Python
Last Updated :
17 Jan, 2025
We need to sort the elements at even indices in increasing order and the elements at odd indices in decreasing order. For example, given a list [10, 3, 5, 8, 2, 7, 6, 1], the result should be [2, 8, 5, 7, 6, 3, 10, 1]. Let us explore different ways to achieve this in Python.
Using List Comprehension with Sorting
This method uses list comprehension to separate even-indexed and odd-indexed elements, sorts them independently, and reconstructs the list.
Python
arr = [10, 3, 5, 8, 2, 7, 6, 1]
even = sorted(arr[i] for i in range(len(arr)) if i % 2 == 0)
odd = sorted((arr[i] for i in range(len(arr)) if i % 2 != 0), reverse=True)
res = [even.pop(0) if i % 2 == 0 else odd.pop(0) for i in range(len(arr))]
print("Resultant list:", res)
OutputResultant list: [2, 8, 5, 7, 6, 3, 10, 1]
Explanation:
- even contains elements at even indices sorted in increasing order.
- odd contains elements at odd indices sorted in decreasing order.
- The list is reconstructed by alternating elements from even and odd.
Using zip() with Separate Sorting
This method uses slicing along with zip() to extract even and odd indexed elements, sorts them and then combines them back.
Python
arr = [10, 3, 5, 8, 2, 7, 6, 1]
even = sorted(arr[::2])
odd = sorted(arr[1::2], reverse=True)
res = [even.pop(0) if i % 2 == 0 else odd.pop(0) for i in range(len(arr))]
print("Resultant list:", res)
OutputResultant list: [2, 8, 5, 7, 6, 3, 10, 1]
Explanation:
- Slicing directly extracts even and odd indexed elements.
- These slices are sorted separately and then combined.
Using Itertools
For Index-Based Reconstruction this method uses iterators to alternate between sorted lists during reconstruction.
Python
from itertools import cycle
arr = [10, 3, 5, 8, 2, 7, 6, 1]
even = iter(sorted(arr[::2]))
odd = iter(sorted(arr[1::2], reverse=True))
res = [next(even) if i % 2 == 0 else next(odd) for i in range(len(arr))]
print(res)
OutputResultant list: [2, 8, 5, 7, 6, 3, 10, 1]
Explanation:
- Iterators ensure efficient traversal of sorted elements.
- The list is reconstructed by alternating between even and odd indexed elements.
Using Heap
This method uses heaps for sorting elements efficiently.
Python
import heapq
arr = [10, 3, 5, 8, 2, 7, 6, 1]
even = [arr[i] for i in range(len(arr)) if i % 2 == 0]
odd = [arr[i] for i in range(len(arr)) if i % 2 != 0]
heapq.heapify(even)
heapq._heapify_max(odd)
result = []
for i in range(len(arr)):
if i % 2 == 0: result.append(heapq.heappop(even))
else:
res.append(heapq._heappop_max(odd))
print(res)
OutputResultant list: [2, 5, 6, 10, 8]
Explanation:
- Heaps are used for efficient sorting of elements.
- Elements are extracted alternately to reconstruct the list.
Similar Reads
Python | Sort Tuples in Increasing Order by any key Given a tuple, sort the list of tuples in increasing order by any key in tuple. Examples: Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)] m = 0 Output : [(1, 2), (2, 3), (2, 5), (4, 4)] Explanation: Sorted using the 0th index key. Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)] m = 2 Output : So
3 min read
Sort a List According to the Length of the Elements - Python We are given a list of strings and our task is to sort the list based on the length of each string. For example, if the input is ["Python", "C", "Java", "JavaScript", "Go"], the output should be ["C", "Go", "Java", "Python", "JavaScript"] since shorter strings come first.Using sorted() sorted() func
3 min read
How to use numpy.argsort in Descending order in Python The numpy.argsort() function is used to conduct an indirect sort along the provided axis using the kind keyword-specified algorithm. It returns an array of indices of the same shape as arr, which would be used to sort the array. It refers to value indices ordered in ascending order. In this article,
4 min read
Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat
2 min read
Python program to Sort a list according to the second element of sublist Sorting a list by the second element of each sublist means rearranging the sublists based on the value at index 1. For example, given the list [[1, 3], [2, 1], [4, 2], [3, 5]], sorting by the second element results in [[2, 1], [4, 2], [1, 3], [3, 5]], where the second elements (1, 2, 3, 5) are in as
3 min read
Python | Sort an array according to absolute difference Given an array of N distinct elements and a number val, rearrange the array elements according to the absolute difference with val, i. e., element having minimum difference comes first and so on. Also the order of array elements should be maintained in case two or more elements have equal difference
3 min read