Open In App

Sort Even and Odd Placed Elements in Increasing Order

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
[4, 1, 7, 2, 8, 3]

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)

Output
[4, 1, 7, 2, 8, 3]

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)

Output
[4, 1, 7, 2, 8, 3]

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)

Output
[4, 1, 7, 2, 8, 3]

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)

Output
[4, 1, 7, 2, 8, 3]

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.

Next Article

Similar Reads