-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathSortColors.java
More file actions
33 lines (31 loc) · 750 Bytes
/
SortColors.java
File metadata and controls
33 lines (31 loc) · 750 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
26
27
28
29
30
31
32
33
package com.sanket.Array;
/*
Problem Link - https://leetcode.com/problems/sort-colors/
Status - Accepted
*/
public class SortColors {
public void sortColors(int[] nums) {
int size = nums.length;
int low = 0, high = size - 1;
int i = low;
while (i <= high) {
if (nums[i] == 2) {
swap(nums, i, high);
high--;
}
else if (nums[i] == 0) {
swap(nums, i, low);
low++;
i++;
}
else {
i++;
}
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}