Python Program to Sort a List Containing Two Types of Elements

In the previous article, we have discussed Python Program to Replace Every List Element by Multiplication of Previous and Next
Given a random list of 0s and 1s and the task is to separate the list into 0s on the left and 1s on the right. Only traverse the list once.

Examples:

Example1:

Input:

Given List = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 1 1 1 1 1 1

Example2:

Input:

Given List = [1 0 1 0 0 0 1 1 0 1]

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 0 1 1 1 1 1

Program to Sort a List Containing Two Types of Elements in Python

Below are the ways to separate the given list into 0s on the left and 1s on the right.

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.
  • Take a variable say ‘p’ and initialize its value with 0.
  • Take another variable ‘q’ and initialize its value with the length of the given list -1.
  • Store it in another variable.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, check again if the value of the given list of p is equal to 1 using the if conditional statement.
  • If the statement is true, then assign a given list of p with the given list of q and a given list of q with the given list of p.
  • Decrement the value of q by 1 and store it in the same variable ‘q’
  • Else increment the value of p by 1 and store it in the same variable ‘p’.
  • Exit the while Loop.
  • Loop from 0 to the length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
givn_lst = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]
# Calculate the length of the given list using the len() function and store it in
# another variable.
lent_lst = len(givn_lst)
# Take a variable say 'p' and initialize its value with 0.
p = 0
# Take another variable 'q' and initialize its value with the length of the given list -1.
# Store it in another variable.
q = lent_lst - 1
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, check again if the value of the given list of p is equal
    # to 1 using the if conditional statement.
    if(givn_lst[p] == 1):
       # If the statement is true, then assign a given list of p with the given list of q and
       # a given list of q with the given list of p.
        givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]
 # Decrement the value of q by 1 and store it in the same variable 'q
        q -= 1
 # Else increment the value of p by 1 and store it in the same variable 'p'.
 # Exit the while Loop.
    else:
        p += 1
print("The above given list after separation of 0s on the left and 1s on the right:")
# Loop from 0 to the length of the given list using the for loop.
for itror in range(0, lent_lst):
        # Inside the loop, print the given list of iterator values.
    print(givn_lst[itror], end=" ")

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 1 1 1 1 1 1

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.
  • Take a variable say ‘p’ and initialize its value with 0.
  • Take another variable ‘q’ and initialize its value with the length of the given list -1.
  • Store it in another variable.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, check again if the value of the given list of p is equal to 1 using the if conditional statement.
  • If the statement is true, then assign a given list of p with the given list of q and a given list of q with the given list of p.
  • Decrement the value of q by 1 and store it in the same variable ‘q’
  • Else increment the value of p by 1 and store it in the same variable ‘p’.
  • Exit the while Loop.
  • Loop from 0 to the length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values.
  • 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.
givn_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.
lent_lst = len(givn_lst)
# Take a variable say 'p' and initialize its value with 0.
p = 0
# Take another variable 'q' and initialize its value with the length of the given list -1.
# Store it in another variable.
q = lent_lst - 1
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, check again if the value of the given list of p is equal
    # to 1 using the if conditional statement.
    if(givn_lst[p] == 1):
       # If the statement is true, then assign a given list of p with the given list of q and
       # a given list of q with the given list of p.
        givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]
 # Decrement the value of q by 1 and store it in the same variable 'q
        q -= 1
 # Else increment the value of p by 1 and store it in the same variable 'p'.
 # Exit the while Loop.
    else:
        p += 1
print("The above given list after separation of 0s on the left and 1s on the right:")
# Loop from 0 to the length of the given list using the for loop.
for itror in range(0, lent_lst):
        # Inside the loop, print the given list of iterator values.
    print(givn_lst[itror], end=" ")

Output:

Enter some random List Elements separated by spaces = 1 0 1 0 0 0 1 1 0 1
The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 0 1 1 1 1 1

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.