-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSortArrayOf012.py
40 lines (34 loc) · 1.07 KB
/
SortArrayOf012.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
"""
@author Anirudh Sharma
Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.
Try to solve this problem in single pass with O(1) space
"""
def sort012(nums):
# Start and end pointers
start = 0
end = len(nums) - 1
# Index to point current element
currentIndex = 0
# Loop through until the pointers meet
while currentIndex <= end and start < end:
# Check if the current element is 0
if nums[currentIndex] == 0:
# Put whatever at the start at the current index
nums[currentIndex] = nums[start]
nums[start] = 0
start += 1
currentIndex += 1
# Check if the current element is 2
elif nums[currentIndex] == 2:
nums[currentIndex] = nums[end]
nums[end] = 2
end -= 1
# If the current element is 1
else:
currentIndex += 1
return nums
if __name__ == "__main__":
print(sort012([2, 0, 2, 1, 1, 0]))
print(sort012([2, 0, 1]))
print(sort012([1]))
print(sort012([0]))