function to sort a binary list in linear time

Solutions on MaxInterview for function to sort a binary list in linear time by the best coders in the world

showing results for - "function to sort a binary list in linear time"
Roberto
20 Jul 2016
1def sort(A):
2 
3    # count number of 0's
4    zeros = A.count(0)
5 
6    # put 0's at the beginning
7    k = 0
8    while zeros:
9        A[k] = 0
10        zeros = zeros - 1
11        k = k + 1
12 
13    # fill all remaining elements by 1
14    for k in range(k, len(A)):
15        A[k] = 1