-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortZerosOnes.py
41 lines (36 loc) · 871 Bytes
/
SortZerosOnes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def sortZerosOnes(arr):
zero = 0
ones = 0
for i in range(len(arr)):
#print("i", i, "zero", zero, "ones", ones)
if arr[i]:
zero += 1
else:
arr[ones], arr[zero] = arr[zero], arr[ones]
ones += 1
zero += 1
#print("arr", arr)
print(arr)
def sortColors(arr):
zero = 0
ones = 0
twos = 0
for i in range(len(arr)):
if arr[i] != 0: #0
ones += 1
twos += 1
elif arr[i] != 1: #1
zero += 1
twos += 1
else: #2
zero += 1
ones += 1
if zero < ones < twos:
continue
else:
if zero > ones:
arr[ones], arr[ones] = arr[zero], arr[ones]
if ones > twos:
arr = \
[1, 1, 0, 1, 0, 0, 2, 1, 0, 0, 2]
sortZerosOnes(arr)