Sorting an array in waveform using Python

In this article, we will learn how to sort an array in waveform using Python. A waveform array follows the pattern where arr[0] >= arr[1] = arr[3] = ..., creating alternating peaks and valleys.

What is Waveform Sorting?

An array is sorted in wave form if elements alternate between being greater than or equal to their neighbors. For example, [4, 3, 12, 6, 25, 15] forms a wave pattern where 4 > 3 6 15.

Method 1: Using Built-in sort() Function

This approach first sorts the array, then swaps adjacent pairs to create the wave pattern ?

Algorithm

  • Sort the input array in ascending order

  • Traverse the array with step size 2

  • Swap adjacent elements to create the wave pattern

Example

def sort_in_waveform_method1(arr):
    # Sort the array in ascending order
    arr.sort()
    
    # Swap adjacent elements starting from index 0
    for i in range(0, len(arr) - 1, 2):
        arr[i], arr[i + 1] = arr[i + 1], arr[i]

# Input array
numbers = [12, 45, 15, 4, 6, 70, 68, 3, 25]
print("Original array:", numbers)

# Create a copy to preserve original
wave_array = numbers.copy()
sort_in_waveform_method1(wave_array)

print("Wave form array:", wave_array)
Original array: [12, 45, 15, 4, 6, 70, 68, 3, 25]
Wave form array: [4, 3, 12, 6, 25, 15, 68, 45, 70]

Time Complexity: O(n log n) due to the sorting step.

Method 2: Single Pass Without Sorting

This optimized approach creates the wave pattern in a single traversal without pre-sorting ?

Algorithm

  • Traverse only even indices of the array

  • For each even index, ensure it's greater than both adjacent elements

  • Swap elements when necessary to maintain the wave property

Example

def sort_in_waveform_method2(arr):
    n = len(arr)
    
    # Traverse all even indices
    for i in range(0, n, 2):
        # Check if current element is smaller than previous
        if i > 0 and arr[i] < arr[i - 1]:
            arr[i], arr[i - 1] = arr[i - 1], arr[i]
        
        # Check if current element is smaller than next
        if i < n - 1 and arr[i] < arr[i + 1]:
            arr[i], arr[i + 1] = arr[i + 1], arr[i]

# Input array
numbers = [12, 45, 15, 4, 6, 70, 68, 3, 25]
print("Original array:", numbers)

# Create a copy to preserve original
wave_array = numbers.copy()
sort_in_waveform_method2(wave_array)

print("Wave form array:", wave_array)
Original array: [12, 45, 15, 4, 6, 70, 68, 3, 25]
Wave form array: [45, 12, 15, 4, 70, 6, 68, 3, 25]

Time Complexity: O(n) as we traverse the array only once.

Comparison

Method Time Complexity Space Complexity Approach
Built-in sort() O(n log n) O(1) Sort then swap pairs
Single pass O(n) O(1) Direct wave formation

Conclusion

The single-pass method is more efficient with O(n) complexity compared to the sort-based approach with O(n log n). Both methods successfully create waveform arrays, but Method 2 is optimal for large datasets due to its linear time complexity.

Updated on: 2026-03-27T00:09:01+05:30

763 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements