Program for Alternative Sorting

Python Program for Alternative Sorting

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, 3, 7, 8, 1, 3, 6]

Output:

The Alternative sorting of the above given list is :
8 1 7 2 6 3 3

Example2:

Input:

Given List = [2, 1, 7, 4, 1, 2, 5]

Output:

The Alternative sorting of the above given list is :
7 1 5 1 4 2 2

Program for Alternative Sorting in Python

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.

Method #1: Using While Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Sort the given list using the sort() method and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with zero.
  • Take another variable say ‘q’ and initialize its value with the length of the given list -1.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, then print the value of the given list of q.
  • Subtract 1 from q (q-1)and store it in the same variable ‘q’.
  • Print the value of the given list of p.
  • Increment the value of p by 1 and store it in the same variable ‘p’.
  • Check if the length of the given list is odd(len_lst % 2 != 0) by using the if conditional statement.
  • If the statement is true, print the value of the given list of p.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [2, 3, 7, 8, 1, 3, 6]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Sort the given list using the sort() method and store it in another variable.
gvn_lst.sort()
# Take a variable say 'p' and initialize its value with zero.
p = 0
# Take another variable say 'q' and initialize its value with the length of the
# given list -1.
q = len_lst-1
print("The Alternative sorting of the above given list is :")
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, then print the value of the given list of q.
    print(gvn_lst[q], end=" ")
    # Subtract 1 from q (q-1)and store it in the same variable 'q'.
    q -= 1
    # Print the value of the given list of p.
    print(gvn_lst[p], end=" ")
    # Increment the value of p by 1 and store it in the same variable 'p'.
    p += 1
   # Check if the length of the given list is odd(len_lst % 2 != 0) by using the
   # if conditional statement.
if (len_lst % 2 != 0):
    # If the statement is true, print the value of the given list of p.
    print(gvn_lst[p])

Output:

The Alternative sorting of the above given list is :
8 1 7 2 6 3 3

Method #2: Using While loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Sort the given list using the sort() method and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with zero.
  • Take another variable say ‘q’ and initialize its value with the length of the given list -1.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, then print the value of the given list of q.
  • Subtract 1 from q (q-1)and store it in the same variable ‘q’.
  • Print the value of the given list of p.
  • Increment the value of p by 1 and store it in the same variable ‘p’.
  • Check if the length of the given list is odd(len_lst % 2 != 0) by using the if conditional statement.
  • If the statement is true, print the value of the given list of p.
  • 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()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Sort the given list using the sort() method and store it in another variable.
gvn_lst.sort()
# Take a variable say 'p' and initialize its value with zero.
p = 0
# Take another variable say 'q' and initialize its value with the length of the
# given list -1.
q = len_lst-1
print("The Alternative sorting of the above given list is :")
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, then print the value of the given list of q.
    print(gvn_lst[q], end=" ")
    # Subtract 1 from q (q-1)and store it in the same variable 'q'.
    q -= 1
    # Print the value of the given list of p.
    print(gvn_lst[p], end=" ")
    # Increment the value of p by 1 and store it in the same variable 'p'.
    p += 1
   # Check if the length of the given list is odd(len_lst % 2 != 0) by using the
   # if conditional statement.
if (len_lst % 2 != 0):
    # If the statement is true, print the value of the given list of p.
    print(gvn_lst[p])

Output:

Enter some random List Elements separated by spaces = 2 1 7 4 1 2 5
The Alternative sorting of the above given list is :
7 1 5 1 4 2 2

 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.