From 0744fb0f4c3153ed1ff24209e0ed7bfe8e74e812 Mon Sep 17 00:00:00 2001 From: Naman Verma Date: Thu, 31 Oct 2024 00:28:47 +0530 Subject: [PATCH] Sort_Colors #LeetCode75 in JAVA Solution for Leetcode problem no.75 [SORT-COLORS] --- Java/SortColors.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Java/SortColors.java 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; + } +}