码迷,mamicode.com
首页 > 其他好文 > 详细

Partition an array around an interger

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

标签:style   blog   http   color   2014   art   

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

标签:style   blog   http   color   2014   art   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!