In the previous article, we have discussed Python Program for Alternative Sorting.
Waveform:
Sort an unsorted list of integers into a wave-like list given an unsorted list of integers.
If list[0..n-1] >= list[1] <= list[2] >= list[3] <= list[4] >=….., a ‘list[0..n-1]’ is sorted in wave form.
Examples:
Example1:
Input:
Given List = [1, 5, 7, 2, 4, 6, 9]
Output:
The above given list after sorted in waveform : 2 1 5 4 7 6 9
Example2:
Input:
Given List = [12, 34, 10, 8, 90, 75, 89]
Output:
The above given list after sorted in waveform : 10 8 34 12 89 75 90
- Python Program to Find Median of List
- Python Program to Merge Two Lists and Sort it | How to Create a Sorted Merged List of Two Unsorted Lists in Python?
- Python Program to Sort the List According to the Second Element in Sublist
Program to Sort a List in Wave Form in Python
Below are the ways to sort a given list in the form of the waveform:
Method #1: Using For Loop (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Sort the above-given list using the sort() function.
- Calculate the length of the given list using the len() function and store it in another variable.
- Loop from 0 to length of the given list with a step size of 2 using the for loop.
- Inside the loop, assign a given list of (iterator +1) values to the given list of iterator and a given list of the iterator to the given list of iterator+1.
- Loop from 0 to length of the given list using the for loop.
- Inside the loop, print the given list of iterator values to sort the list in a waveform.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = [1, 5, 7, 2, 4, 6, 9] # Sort the above-given list using the sort() function. gvn_lst.sort() # Calculate the length of the given list using the len() function and # store it in another variable. len_lst = len(gvn_lst) # Loop from 0 to length of the given list with a step size of 2 using the for loop. for itr in range(0, len_lst-1, 2): # Assign a given list of (iterator +1) values to the given list of iterator and # a given list of the iterator to the given list of iterator+1. gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr] print("The above given list after sorted in waveform :") # Loop from 0 to length of the given list using the for loop. for itr in range(0, len(gvn_lst)): # Inside the loop, print the given list of iterator values to sort the list in a waveform. print(gvn_lst[itr], end=' ')
Output:
The above given list after sorted in waveform : 2 1 5 4 7 6 9
Method #2: Using For loop (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Sort the above-given list using the sort() function.
- Calculate the length of the given list using the len() function and store it in another variable.
- Loop from 0 to length of the given list with a step size of 2 using the for loop.
- Assign a given list of (iterator +1) values to the given list of iterator and a given list of the iterator to the given list of iterator+1.
- Loop from 0 to length of the given list using the for loop.
- Inside the loop, print the given list of iterator values to sort the list in a waveform.
- The Exit of the Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. #Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Sort the above-given list using the sort() function. gvn_lst.sort() # Calculate the length of the given list using the len() function and # store it in another variable. len_lst = len(gvn_lst) # Loop from 0 to length of the given list with a step size of 2 using the for loop. for itr in range(0, len_lst-1, 2): # Assign a given list of (iterator +1) values to the given list of iterator and # a given list of the iterator to the given list of iterator+1. gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr] print("The above given list after sorted in waveform :") # Loop from 0 to length of the given list using the for loop. for itr in range(0, len(gvn_lst)): # Inside the loop, print the given list of iterator values to sort the list in a waveform. print(gvn_lst[itr], end=' ')
Output:
Enter some random List Elements separated by spaces = 15 16 17 1 2 3 The above given list after sorted in waveform : 2 1 15 3 17 16
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
- Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words in a Hyphen-Separated Sequence after Sorting them Alphabetically
- Program for Bubble Sort in Python
- Python Program to Sort a List Containing Two Types of Elements
- Python Program for Alternative Sorting.
- Python Program to Check if Array can be Sorted with One Swap