Skip to content

Commit 248d04c

Browse files
committed
sort by parity
1 parent 77733b5 commit 248d04c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Vmware/sort_array_by_parity.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Given an array A of non-negative integers, return an array consisting
3+
of all the even elements of A, followed by all the odd elements of A.
4+
"""
5+
6+
def sortArrayByParity(A):
7+
ptr, i = 0, 0
8+
while i < len(A):
9+
if A[i] % 2 == 0:
10+
A[ptr], A[i] = A[i], A[ptr]
11+
ptr += 1
12+
i += 1
13+
return A
14+
A = [1,6,8,3,0,2,3,4,5,6,7,9]
15+
print(sortArrayByParity(A))

0 commit comments

Comments
 (0)