首页 > 其他 > 详细

Partition an array around an interger

时间:2014-07-15 22:51:50      阅读:438      评论:0      收藏:0      [点我收藏+]

Partition an array of integers around a value such taht all elements less than x come before elements greater than or equal to x.

Idea: Just use of subroutine PARTION of quicksort() with a slight modification.

array[start, end] to be partitioned around value x.

We need to additional pointers i and j, to maintain an invariant, which is true at all times:

  array[start, i] stores elements less than x

  array[i+1, j-1] stores elements greater than or equal to x

  array[j, end] stores elements which haven‘t been explored yet.

   bubuko.com,布布扣

void Partition(A, start, end){
    int i = start - 1;
    for(int j = start; j <= end; ++j){
        if(A[j] < x){
            i++;
            swap A[i] with A[j];
        }
    }
}

 

Partition an array around an interger,布布扣,bubuko.com

Partition an array around an interger

原文:http://www.cnblogs.com/Antech/p/3843878.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!