Sort Even and Odd Placed Elements in Increasing Order
Last Updated :
20 Jan, 2025
Given a list, we need to sort the elements at even and odd positions in increasing order. For example, sort the elements at even indices (0, 2, 4, ...) and odd indices (1, 3, 5, ...) separately in increasing order, then combine them while maintaining their original positions.
Using index slicing (Most Efficient)
This method uses slicing to separate even and odd indexed elements, sorts them, and then merges the results back into the original list.
Python
# Initializing the list
a = [7, 1, 4, 3, 8, 2]
# Separating even and odd indexed elements
even = a[0::2]
odd = a[1::2]
# Sorting both parts
even.sort()
odd.sort()
# Replacing sorted values in original list
a[0::2] = even
a[1::2] = odd
print(a)
Explanation:
- Slicing is used to split the list into even and odd indexed elements.
- Sorting is done on smaller subsets, making the process efficient.
Let's explore some more ways and see how we can sort even and odd placed elements in increasing order in Python.
Using sorted() with lambda
Here, we use the sorted() function with a lambda function to sort even and odd indexed elements separately.
Python
# Initializing the list
a = [7, 1, 4, 3, 8, 2]
# Sorting and replacing values directly
a[0::2] = sorted(a[0::2])
a[1::2] = sorted(a[1::2])
print(a)
Explanation: The sorted() function is applied directly to slices, avoiding the need for temporary variables.
Using for loop
We iterate through the list to separate even and odd indexed elements, sort them, and then reassign their positions.
Python
# Initializing the list
a = [7, 1, 4, 3, 8, 2]
# Initializing separate lists for even and odd indexed elements
even = []
odd = []
# Splitting the elements
for i in range(len(a)):
if i % 2 == 0:
even.append(a[i])
else:
odd.append(a[i])
# Sorting both lists
even.sort()
odd.sort()
# Replacing sorted values in the original list
j = 0
k = 0
for i in range(len(a)):
if i % 2 == 0:
a[i] = even[j]
j += 1
else:
a[i] = odd[k]
k += 1
print(a)
Explanation:
- The loop separates even and odd indexed elements, which are then sorted.
- Replacing sorted elements ensures the correct order is maintained in the list.
Using heapq
The heapq module can sort the elements while maintaining their original indices.
Python
import heapq
# Initializing the list
a = [7, 1, 4, 3, 8, 2]
# Separating and sorting even and odd indexed elements
even = a[0::2]
odd = a[1::2]
heapq.heapify(even)
heapq.heapify(odd)
# Replacing sorted values in original list
for i in range(0, len(a), 2):
a[i] = heapq.heappop(even)
for i in range(1, len(a), 2):
a[i] = heapq.heappop(odd)
print(a)
Explanation:
- heapq.heapify is used for sorting subsets efficiently.
- This is suitable for scenarios requiring minimal memory overhead.
Sorting in-place with nested loops
This method sorts the list without creating additional lists, but it is slower due to nested loops.
Python
# Initializing the list
a = [7, 1, 4, 3, 8, 2]
# Sorting even indexed elements in-place
for i in range(0, len(a), 2):
for j in range(i + 2, len(a), 2):
if a[i] > a[j]:
a[i], a[j] = a[j], a[i]
# Sorting odd indexed elements in-place
for i in range(1, len(a), 2):
for j in range(i + 2, len(a), 2):
if a[i] > a[j]:
a[i], a[j] = a[j], a[i]
print(a)
Explanation:
- Sorting is performed directly in the list using nested loops for even and odd indices.
- This method avoids using additional lists, but it is less efficient for large inputs.
Similar Reads
Python Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
7 min read
Python Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If the
3 min read
Check if elements of an array can be arranged satisfying the given condition Given an array arr of N (even) integer elements. The task is to check if it is possible to reorder the elements of the array such that: arr[2*i + 1] = 2 * A[2 * i] for i = 0 ... N-1. Print True if it is possible, otherwise print False. Examples: Input: arr[] = {4, -2, 2, -4} Output: True {-2, -4, 2,
5 min read
Find if array can be sorted by swaps limited to multiples of k Given an array and a number k, the task is to check if the given array can be sorted or not with limited swap operations. We can swap arr[i] only with arr[i] or arr[i + k] or arr[i + 2*k] or arr[i + 3*k] and so on. In general an element at index i can be swapped with elements at indexes i + j*k wher
10 min read
Python3 Program to Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i Given an array of n elements. Our task is to write a program to rearrange the array such that elements at even positions are greater than all elements before it and elements at odd positions are less than all elements before it.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 7}Output : 4 5 3 6 2 7 1Inp
3 min read