【问题标题】:Insertionsort for Array with permutation (Java)带有排列的数组的插入排序(Java)
【发布时间】:2020-05-14 10:25:34
【问题描述】:

所以我有这个数组,我必须使用 Insertionssort 进行排序:

( 4 1 2 3 )= arr[0] ,
( 1 2 3 4 )=arr[1] ,
( 4 3 2 1 ) = arr[2], 
( 3 1 2 4 )= arr[3] ,
( 4 3 1 2 ) = arr[4] ,
( 2 1 3 4 ) = arr[5] ,
( 4 1 3 2 ) = arr[6] ,
( 1 4 2 3 )= arr[7] 

如果数组 1 的值 1-4 与数组 2 的值 1-4 之间的差异较大,则一个数组将与另一个交换。例如:arr[0] arr[5].value(1 ) --> 交换。

所以排序后的数组将如下所示:

(3,1,2,4) < (4,1,2,3) < (1,4,2,3) < (2,1,3,4) < (4,1,3,2) < (4,3,1,2) < (1,2,3,4) < (4,3,2,1)

我有这个方法 atm,这里只检查了第一个条件:

public int insertionsort(permutation[] arr, int gap) {
            int count = 0;
                for (int i = gap; i<arr.length-1; i++) {
                    count++;
                    permutation new = arr[i];
                    int k = i;
                    System.out.println("K: " + k);
                    System.out.println("gap: "+ gap);
                    while(Math.abs(arr[i].value(1)-arr[i].value(4)) > Math.abs(arr[i-gap].value(1) - 
                    arr[i-gap].value(4))) {
                        arr[k] = arr[k-gap];
                        k = k-gap;
                        System.out.println("k-gap: " + (k-gap));
                    }
                    arr[k] = new;
                }

        return count;
        } 

现在我认为我的数组会首先按照小差异的顺序进行排序,但它似乎不正确。我希望你能帮助我!

【问题讨论】:

  • 你试过调试代码了吗?
  • 什么是permutation? --- insertionsort 怎么称呼?

标签: java arrays permutation


【解决方案1】:

我不清楚gap 参数的用途是什么,希望您可以根据需要合并它,但这里是标准Insertion Sort 方法的Java 直接翻译。

static void sort(permutation[] perms)
{
    for(int i=1; i<perms.length; i++)
    {
        permutation fixed = perms[i];
        int j = i - 1;
        while(j >= 0 && perms[j].compareTo(fixed) > 0)
        {
            perms[j+1] = perms[j];
            j -= 1;
        }
        perms[j+1] = fixed;
    }
}

猜测permutation 类可能是什么样子:

static class permutation
{
    int len;
    int[] arr;

    public permutation(int... vals)
    {
        len = vals.length;
        arr = vals.clone();
    }

    int value(int pos) 
    {
        return arr[pos-1];
    }

    public int compareTo(permutation p)
    {
        int cmp = Math.abs(value(1)-value(len)) - Math.abs(p.value(1)-p.value(len));
        if(cmp == 0)
            for(int i=1; cmp==0 && i<=len; i++)
                cmp = value(i)-p.value(i);
        return cmp;
    }
}

测试:

permutation[] perms = {
        new permutation(4,1,2,3),
        new permutation(1,2,3,4),
        new permutation(4,3,2,1),
        new permutation(3,1,2,4),
        new permutation(4,3,1,2),
        new permutation(2,1,3,4),
        new permutation(4,1,3,2),
        new permutation(1,4,3,2)
};

sort(perms);

for(permutation p : perms)
    System.out.println(Arrays.toString(p.arr));

输出:

[1, 4, 3, 2]
[3, 1, 2, 4]
[4, 1, 2, 3]
[2, 1, 3, 4]
[4, 1, 3, 2]
[4, 3, 1, 2]
[1, 2, 3, 4]
[4, 3, 2, 1]

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 2023-03-30
    • 1970-01-01
    • 2021-03-01
    • 2023-03-06
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多