-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort Colors.py
More file actions
25 lines (18 loc) · 778 Bytes
/
Sort Colors.py
File metadata and controls
25 lines (18 loc) · 778 Bytes
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
#!/usr/bin/python
'''
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library's sort function.
'''
def main():
nums = list(map(int, input("Enter the numbers: ").strip().split()))
swap = True
while swap:
swap = False
for i in range(len(nums) - 1):
if nums[i]> nums[i + 1]:
nums[i], nums[i + 1] = nums[i + 1], nums[i]
swap = True
print(nums)
if __name__ == "__main__":
main()