{"id":18208,"date":"2021-08-26T10:42:11","date_gmt":"2021-08-26T05:12:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=18208"},"modified":"2021-11-22T18:37:14","modified_gmt":"2021-11-22T13:07:14","slug":"python-program-for-alternative-sorting","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-alternative-sorting\/","title":{"rendered":"Python Program for Alternative Sorting"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Sort a List Containing Two Types of Elements<\/a> 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 Print the list so that the first element is the first maximum, the second element is the first minimum, and so on.<\/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 \u00a0<\/strong>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 to Sort a List Containing Two Types of Elements Given a list and the task is to Print the list so that the first element is the first maximum, the second element is the first minimum, and so on. Examples: Example1: Input: Given List = [2, …<\/p>\n
\nGiven a list and the task is to Print the list so that the first element is the first maximum, the second element is the first minimum, and so on.<\/p>\nGiven List = [2, 3, 7, 8, 1, 3, 6]<\/pre>\n
The Alternative sorting of the above given list is :\r\n8 1 7 2 6 3 3<\/pre>\n
Given List = [2, 1, 7, 4, 1, 2, 5]<\/pre>\n
The Alternative sorting of the above given list is :\r\n7 1 5 1 4 2 2<\/pre>\n
Program for Alternative Sorting in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [2, 3, 7, 8, 1, 3, 6]\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# Sort the given list using the sort() method and store it in another variable.\r\ngvn_lst.sort()\r\n# Take a variable say 'p' and initialize its value with zero.\r\np = 0\r\n# Take another variable say 'q' and initialize its value with the length of the\r\n# given list -1.\r\nq = len_lst-1\r\nprint(\"The Alternative sorting of the above given list is :\")\r\n# Check if the value of p is less than q using the while loop.\r\nwhile (p < q):\r\n # If the statement is true, then print the value of the given list of q.\r\n print(gvn_lst[q], end=\" \")\r\n # Subtract 1 from q (q-1)and store it in the same variable 'q'.\r\n q -= 1\r\n # Print the value of the given list of p.\r\n print(gvn_lst[p], end=\" \")\r\n # Increment the value of p by 1 and store it in the same variable 'p'.\r\n p += 1\r\n # Check if the length of the given list is odd(len_lst % 2 != 0) by using the\r\n # if conditional statement.\r\nif (len_lst % 2 != 0):\r\n # If the statement is true, print the value of the given list of p.\r\n print(gvn_lst[p])\r\n<\/pre>\n
The Alternative sorting of the above given list is :\r\n8 1 7 2 6 3 3<\/pre>\n
Method #2: Using While 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# 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# Sort the given list using the sort() method and store it in another variable.\r\ngvn_lst.sort()\r\n# Take a variable say 'p' and initialize its value with zero.\r\np = 0\r\n# Take another variable say 'q' and initialize its value with the length of the\r\n# given list -1.\r\nq = len_lst-1\r\nprint(\"The Alternative sorting of the above given list is :\")\r\n# Check if the value of p is less than q using the while loop.\r\nwhile (p < q):\r\n # If the statement is true, then print the value of the given list of q.\r\n print(gvn_lst[q], end=\" \")\r\n # Subtract 1 from q (q-1)and store it in the same variable 'q'.\r\n q -= 1\r\n # Print the value of the given list of p.\r\n print(gvn_lst[p], end=\" \")\r\n # Increment the value of p by 1 and store it in the same variable 'p'.\r\n p += 1\r\n # Check if the length of the given list is odd(len_lst % 2 != 0) by using the\r\n # if conditional statement.\r\nif (len_lst % 2 != 0):\r\n # If the statement is true, print the value of the given list of p.\r\n print(gvn_lst[p])\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 2 1 7 4 1 2 5\r\nThe Alternative sorting of the above given list is :\r\n7 1 5 1 4 2 2<\/pre>\n
\n