- def MINtech_sort_colors(nums):
- left, right, current = 0, len(nums) - 1, 0
-
- while current <= right:
- if nums[current] == 0:
- nums[current], nums[left] = nums[left], nums[current]
- left += 1
- current += 1
- elif nums[current] == 2:
- nums[current], nums[right] = nums[right], nums[current]
- right -= 1
- else:
- current += 1
-
- if __name__ == "__main__":
- length = int(input("Enter the length of the array: "))
- nums = [int(input(f"Enter element {i + 1} (0, 1, or 2): ")) for i in range(length)]
-
- MINtech_sort_colors(nums)
-
- # Print the sorted array
- print("Sorted Array:", end=" ")
- for num in nums:
- print(num, end=" ")
No comments:
Post a Comment