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

前端 未结 5 1397
梦毁少年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 09:08

    The C++ library has a stable_partition algorithm which requires n comparisons and O(n log n) swaps when it runs in-place.

    As @Ted points out, the problem requires two applications of this algorithm.

提交回复
热议问题