264. Sort an array of 0,1 and 2
Microsoft Interview Questions and Answers
(Continued from previous question...)
264. Sort an array of 0,1 and 2
Question:
Sort an array of 0,1 and 2
maybe an answer:
int[] a = { 2, 0, 1, 1, 2, 0, 2, 2, 0, 1, 1, 2, 1, 0, 2 };
int lo = 0;
int hi = a.Length - 1;
int mid = 0;
while (mid <= hi)
{
switch (a[mid])
{
case 0:
swap(ref a[lo++], ref a[mid++]);
break;
case 1:
mid++;
break;
case 2:
swap(ref a[mid], ref a[hi--]);
break;
}
}
(Continued on next question...)
Other Interview Questions
|