diff --git a/Java/SortColors.java b/Java/SortColors.java new file mode 100644 index 0000000..f9bc204 --- /dev/null +++ b/Java/SortColors.java @@ -0,0 +1,27 @@ +public class SortColors { + public void sortColors(int[] nums) { + int n = nums.length; + int low = 0; + int mid = 0; + int high = n-1; + while(mid<=high){ + if(nums[mid]==0){ + swap(nums,low,mid); + low++; + mid++; + } + else if(nums[mid]==1){ + mid++; + } + else{ + swap(nums,mid,high); + high--; + } + } + } + private void swap(int[] nums, int i, int j){ + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +}