w3resource

Python: Start with a list of integers, keep every other element in place and otherwise sort the list


Sort Keeping Every Other Element Fixed

Write a Python program to start with a list of integers, keep every other element in place and otherwise sort the list.

Input: 
[2, 5, 6, 3, 1, 4, 34]
Output:
[1, 5, 2, 3, 6, 4, 34]

Input:
[8, 0, 7, 2, 9, 4, 1, 2, 8, 3]
Output:
[1, 0, 7, 2, 8, 4, 8, 2, 9, 3]

Sample Solution:

Python Code:

# Define a function named 'test' that takes a list of numbers 'nums' as a parameter
def test(nums):
    # Create a copy of the original list to avoid modifying the input list
    li = nums.copy()    
    
    # Iterate through the elements of the list with even indices
    for i in range(len(li)):
        # Check if the current index is even
        if i % 2 == 0: 
            # Iterate through the elements with even indices after the current index
            for j in range(i + 2, len(li), 2):
                # Check if the element at index 'j' is less than the element at index 'i'
                if li[j] < li[i]:
                    # Call the 'swap' function to swap elements at indices 'i' and 'j'
                    swap(li, i, j)
    
    # Return the modified list
    return li

# Define a function named 'swap' that swaps elements at indices 'i' and 'j' in the given list 'li'
def swap(li, i, j):
    # Temporary variable to store the value at index 'i'
    temp = li[i]
    # Swap the values at indices 'i' and 'j'
    li[i] = li[j]
    li[j] = temp

# Example 1
nums1 = [2, 5, 6, 3, 1, 4, 34]
print("Original list of numbers:")
print(nums1)
print("Keep every other element in place and otherwise sort the list:")
print(test(nums1))

# Example 2
nums2 = [8, 0, 7, 2, 9, 4, 1, 2, 8, 3]
print("\nOriginal list of numbers:")
print(nums2)
print("Keep every other element in place and otherwise sort the list:")
print(test(nums2))

Sample Output:

Original list (triple) of lists:
[2, 5, 6, 3, 1, 4, 34]
In the said list, keep every other element in place and otherwise sort the list.:
[1, 5, 2, 3, 6, 4, 34]

Original list (triple) of lists:
[8, 0, 7, 2, 9, 4, 1, 2, 8, 3]
In the said list, keep every other element in place and otherwise sort the list.:
[1, 0, 7, 2, 8, 4, 8, 2, 9, 3]

Flowchart:

Flowchart: Python - Start with a list of integers, keep every other element in place and otherwise sort the list.

For more Practice: Solve these Related Problems:

  • Write a Python program to sort a list of integers while keeping the elements at even indices fixed in their original positions.
  • Write a Python program to separate the elements at odd indices, sort them, and then reinsert them into the original list.
  • Write a Python program to use slicing to extract elements that are not fixed, sort them, and merge with fixed elements.
  • Write a Python program to implement an alternating sort that leaves every other element in place and sorts the remainder.

Go to:


Previous: Find all n-digit integers that start or end with 2.
Next: Find the closest palindrome.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.