Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将元素插入到已排序的数组中_Java_Algorithm - Fatal编程技术网

Java 将元素插入到已排序的数组中

Java 将元素插入到已排序的数组中,java,algorithm,Java,Algorithm,我编写这个函数是为了将一个元素插入到排序的数组中,这样在添加元素后数组仍然会被排序 但有点不对劲。我知道我的代码有很多边缘情况,可能是算法过于复杂,但我真的想修复它 我的代码: private静态无效插入(E,E[]arr,int count,Comparator comp){ 如果(计数=0)arr[0]=e; for(int i=0;i=0){ //我们发现一个元素>=到e //我们想在索引i处添加新元素,当前arr[i]已被占用 //由更大的元素,所以我们需要调整 如果(i!=0){ 我-

我编写这个函数是为了一个元素插入到排序的数组中,这样在添加元素后数组仍然会被排序

但有点不对劲。我知道我的代码有很多边缘情况,可能是算法过于复杂,但我真的想修复它

我的代码:

private静态无效插入(E,E[]arr,int count,Comparator comp){
如果(计数=0)arr[0]=e;
for(int i=0;i=0){
//我们发现一个元素>=到e
//我们想在索引i处添加新元素,当前arr[i]已被占用
//由更大的元素,所以我们需要调整
如果(i!=0){
我--;
}否则{
//无所事事
}
}否则如果(i+1==计数){
//这是循环的最后一次迭代,所以我们希望在i+1处添加元素
i++;
}否则{
//保持循环以查找元素
继续;
}
//我们需要把元素移到右边来腾出空间
对于(int j=count;j>i;j--){
arr[j]=arr[j-1];
}
arr[i]=e;
打破
}
}

我修复了代码,我不应该减少
I

private <E> void insert(E e, E[] arr, int count, Comparator<E> comp) {
    if (count == 0) {
        arr[0] = e;
        return;
    }

    for (int i = 0; i < count; i++) {
        if (comp.compare(arr[i], e) >= 0) {
            // we found an element that is >= to e
            // we want to add new element at index i, currently arr[i] is occupied
            // by larger element, so we need to adjust
        } else if (i + 1 == count) {
            // this is the last iteration of the loop so we want to add element at i + 1
            i++;
        } else {
            // keep looping to find an element
            continue;
        }

        // we need to move elements to the right to make space
        for (int j = count; j > i; j--) {
            arr[j] = arr[j - 1];
        }

        arr[i] = e;
        break;
    }
}
private void insert(E,E[]arr,int count,Comparator comp){
如果(计数=0){
arr[0]=e;
返回;
}
for(int i=0;i=0){
//我们发现一个元素>=到e
//我们想在索引i处添加新元素,当前arr[i]已被占用
//由更大的元素,所以我们需要调整
}否则如果(i+1==计数){
//这是循环的最后一次迭代,所以我们希望在i+1处添加元素
i++;
}否则{
//保持循环以查找元素
继续;
}
//我们需要把元素移到右边来腾出空间
对于(int j=count;j>i;j--){
arr[j]=arr[j-1];
}
arr[i]=e;
打破
}
}

查看
插入排序的任何实现。它执行完全相同的动作,我明白。大多数实现从右到左查看数组以找到要添加的位置。但是我想从左向右循环。提示:
Arrays.binarySearch
@Node.JS任何方向都没有实现循环!!(除了非常幼稚和可能不正确的)
private <E> void insert(E e, E[] arr, int count, Comparator<E> comp) {
    if (count == 0) {
        arr[0] = e;
        return;
    }

    for (int i = 0; i < count; i++) {
        if (comp.compare(arr[i], e) >= 0) {
            // we found an element that is >= to e
            // we want to add new element at index i, currently arr[i] is occupied
            // by larger element, so we need to adjust
        } else if (i + 1 == count) {
            // this is the last iteration of the loop so we want to add element at i + 1
            i++;
        } else {
            // keep looping to find an element
            continue;
        }

        // we need to move elements to the right to make space
        for (int j = count; j > i; j--) {
            arr[j] = arr[j - 1];
        }

        arr[i] = e;
        break;
    }
}