-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortColors.java
More file actions
30 lines (26 loc) · 950 Bytes
/
sortColors.java
File metadata and controls
30 lines (26 loc) · 950 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
/*Given an array with n objects colored red, white or blue,
sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
*/
public void sortColors(ArrayList<Integer> a) {
if(a.isEmpty() || a ==null )
return;
int low =0, i =0, high=a.size()-1;
while(i< high)
{
if(a.get(i)==0){
int temp = a.get(i);
a.set(i,a.get(low) );
a.set(low, temp);
low++;
}
else if(a.get(i)==2)
{
int temp = a.get(high);
a.set(high,a.get(i) );
a.set(i, temp);
high--;
}
else i++;
}
}