对数组进行连续排序



我有一个用户给了我一个随机的对象数组,我想做一些错误检查,基本上我希望null对象位于数组的末尾,这样数组的中间只由非null对象组成(对象的排序无关紧要)。

这是我的东西,它不起作用。有人能帮忙吗。

private void properArray(){
    int i = 0;
    int j;
    int cap = theHeap.length;
    for(; i < (cap-1); i++){
        if (theHeap[i] == null){
            j = i + 1;
            while(j < (cap-1)){
                if(theHeap[j] != null){
                    theHeap[i] = theHeap[j];
                    theHeap[j] = null;
                }
                j++;  
            }
        }
    } 
}

这里有一种更简单的方法可以对这样的数组进行排序:

Arrays.sort(theHeap, new Comparator() {
  public int compare(Object o1, Object o2) {
    // nulls are "greater" than non-nulls
    if (o1 == null && o2 != null) return 1;
    // non-nulls are "smaller" than nulls
    if (o1 != null && o2 == null) return -1;
    // in all other comparisons, we don't care
    return 0;
  }
});

或者使用Java 8:

Arrays.sort(theHeap, (o1, o2) -> (o1 == null && o2 != null) ?  1
                               : (o1 != null && o2 == null) ? -1
                               :                               0);

如果您的类路径上有ApacheCommonsCollections,您可以用更少的代码来编写:

Arrays.sort(theHeap, new NullComparator());

正如Ted所提到的,这在O(n log n)中执行,并创建数组的克隆以进行排序。。。因此,这不是最快的解决方案。。。

不需要在数组中迭代两次。如果你不关心非空对象的顺序(特别是,如果它们不需要保持相同的相对顺序),你可以非常简单地做到这一点:

int end = theHeap.length;
for (int i = 0; i < end; ++i) {
    while (theHeap[i] == null && i < end) {
        --end;
        theHeap[i] = theHeap[end];
        theHeap[end] = null;
    }
}

由于每个循环迭代(外部或内部)将(end - i)减少一,并且循环在它们相遇时结束,因此这是一个O(N)算法。

EDIT避免交换null的修订版(可能效率稍高):

int end = theHeap.length;
for (int i = 0; i < end; ++i) {
    if (theHeap[i] == null) {
         while (--end > i && theHeap[end] == null) {
             // loop
         }
         if (i < end) {
             theHeap[i] = theHeap[end];
             theHeap[end] = null;
         }
    }
}

EDIT 2一个简单得多的版本,它还维护非null元素的初始排序顺序:

int next = 0;
for (int i = 0; i < theHeap.length; ++i) {
    if (theHeap[i] != null) {
        if (i > next) {
            theHeap[next] = theHeap[i];
            theHeap[i] = null;
        }
        ++next;
    }
}

尝试:

int j = array.length;
for (int i = 0; i < j; ++i) {
  if (array[--j] == null) {
    continue;
  }
  // array[j] is not null.
  if (array[i] == null) {
    array[i] = array[j];
    array[j] = null;
  }
}

最新更新