Answers for "Function to sort a binary list in linear time"

0

Function to sort a binary list in linear time

def sort(A):
 
    # count number of 0's
    zeros = A.count(0)
 
    # put 0's at the beginning
    k = 0
    while zeros:
        A[k] = 0
        zeros = zeros - 1
        k = k + 1
 
    # fill all remaining elements by 1
    for k in range(k, len(A)):
        A[k] = 1
Posted by: Guest on February-02-2021

Code answers related to "Function to sort a binary list in linear time"

Python Answers by Framework

Browse Popular Code Answers by Language