Sorting a Three-Valued Sequence