How to sort an integer array into negative, zero, positive part without changing relative position?

前端 未结 5 1400
梦毁少年i
梦毁少年i 2021-02-04 08:31

Give an O(n) algorithm which takes as input an array S, then divides S into three sets: negatives, zeros, and positives. Show how to implement this in place, that is, without a

5条回答
  •  日久生厌
    2021-02-04 08:55

    I'm not sure if this helps, but the requirement to stably partition into three classes can be reduced to the problem of stably partitioning into two classes: separate the negative from non-negative, then the positive from non-positive. If the two-class problem can be solved in O(1) space and O(n) time, the solution can be applied twice to solve the original problem.

提交回复
热议问题