{"id":18275,"date":"2021-08-26T10:42:08","date_gmt":"2021-08-26T05:12:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=18275"},"modified":"2021-11-22T18:37:15","modified_gmt":"2021-11-22T13:07:15","slug":"python-program-to-sort-a-list-in-wave-form","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-a-list-in-wave-form\/","title":{"rendered":"Python Program to Sort a List in Wave Form"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Alternative Sorting.<\/a> Sort an unsorted list of integers into a wave-like list given an unsorted list of integers.<\/p>\n If list[0..n-1] >= list[1] <= list[2] >= list[3] <= list[4] >=….., a\u00a0 \u2018list[0..n-1]’ is sorted in wave form.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to sort a given list in the form of the waveform:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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\u00a0 \u2018list[0..n-1]’ is sorted in wave form. Examples: Example1: Input: Given List = [1, 5, …<\/p>\n
\nWaveform:<\/strong><\/p>\nGiven List = [1, 5, 7, 2, 4, 6, 9]<\/pre>\n
The above given list after sorted in waveform :\r\n2 1 5 4 7 6 9<\/pre>\n
Given List = [12, 34, 10, 8, 90, 75, 89]<\/pre>\n
The above given list after sorted in waveform :\r\n10 8 34 12 89 75 90<\/pre>\n
Program to Sort a List in Wave Form in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 5, 7, 2, 4, 6, 9]\r\n# Sort the above-given list using the sort() function.\r\ngvn_lst.sort()\r\n# Calculate the length of the given list using the len() function and\r\n# store it in another variable.\r\nlen_lst = len(gvn_lst)\r\n# Loop from 0 to length of the given list with a step size of 2 using the for loop.\r\nfor itr in range(0, len_lst-1, 2):\r\n # Assign a given list of (iterator +1) values to the given list of iterator and\r\n # a given list of the iterator to the given list of iterator+1.\r\n gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]\r\nprint(\"The above given list after sorted in waveform :\")\r\n# Loop from 0 to length of the given list using the for loop.\r\nfor itr in range(0, len(gvn_lst)):\r\n # Inside the loop, print the given list of iterator values to sort the list in a waveform.\r\n print(gvn_lst[itr], end=' ')\r\n<\/pre>\n
The above given list after sorted in waveform :\r\n2 1 5 4 7 6 9<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n#Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Sort the above-given list using the sort() function.\r\ngvn_lst.sort()\r\n# Calculate the length of the given list using the len() function and\r\n# store it in another variable.\r\nlen_lst = len(gvn_lst)\r\n# Loop from 0 to length of the given list with a step size of 2 using the for loop.\r\nfor itr in range(0, len_lst-1, 2):\r\n # Assign a given list of (iterator +1) values to the given list of iterator and\r\n # a given list of the iterator to the given list of iterator+1.\r\n gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]\r\nprint(\"The above given list after sorted in waveform :\")\r\n# Loop from 0 to length of the given list using the for loop.\r\nfor itr in range(0, len(gvn_lst)):\r\n # Inside the loop, print the given list of iterator values to sort the list in a waveform.\r\n print(gvn_lst[itr], end=' ')\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 15 16 17 1 2 3\r\nThe above given list after sorted in waveform :\r\n2 1 15 3 17 16<\/pre>\n
\n