void sort01OnePass(int input[], int count) {
if ((input == NULL) || (count == 0)) return;
int i0 = 0;
int i1 = 1;
while (true) {
while ((i0 < count) && (input[i0] == 0)) {
i0 += 2;
}
if (i0 >= count) break;
while ((i1 < count) && (input[i1] == 1)) {
i1 += 2;
}
if (i1 >= count) break;
input[i0] = 0;
input[i1] = 1;
}
if (i0 >= count) { //We have all even positions set to 0, now we need to take care of the odd positions left.
int iend = (count % 2 ==0)? (count - 1):(count - 2);
while (true) {
while ((i1 < iend) && (input[i1] == 1))
i1 +=2;
while ((iend > i1) && (input[iend] == 0))
iend -= 2;
if (i1 >= iend) break;
input[i1] = 1;
input[iend] = 0;
}
} else { //We have all odd numbers set to 1, now we need to take care of the even positions left.
int iend = (count % 2 == 0)? (count - 2): (count - 1);
while (true) {
while ((i0 < iend) && (input[i0] == 0))
i0 += 2;
while ((iend > i0) && (input[iend] == 1)) {
iend -= 2;
}
if (i0 >= iend) break;
input[i0] = 0;
input[iend] = 1;
}
}
}