gpt4 book ai didi

java - 目的是防止主体/用户从内存中学习,你明白那是什么吗?

转载 作者:行者123 更新时间:2023-12-02 12:31:06 25 4
gpt4 key购买 nike

附件是开发的合并排序代码,它能够计算对给定数组进行的比较并打印出每个比较。我希望它能够有效地阻止用户学习,以便比较的数字有足够的间隔,这样人们就不会记得看到过过去的数字。现在我们需要让它以不同的顺序进行这些比较。我预计,代码可能不再漂亮和递归,而是一些巨大的丑陋循环。

public class MyMergeSort {
private int[] array; //array declared
private int[] tempMergArr; //temporary array
private int length; //counting length of array
private int ncompare=0;

public static void main(String a[]){

int[] inputArr = {9, 2, 56, 5, 4, 6, 60, 8, 1, //the array, given 60
32, 21, 12, 42, 57, 15, 16, 50, 18, 19,
20, 11, 34, 23, 48, 25, 26, 27, 51, 29,
30, 31, 10, 33, 22, 35, 39, 37, 38, 36,
40, 41, 13, 43, 44, 53, 46, 47, 24, 49,
17, 28, 52, 45, 54, 55, 3, 14, 58, 59,
7};

MyMergeSort mms = new MyMergeSort(); //declaring the merge sort for the array
mms.sort(inputArr);
System.out.print("\n\n");
for(int i:inputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.print("\n Number of comparisons "+mms.ncompare+"\n");
}

public void sort(int inputArr[]) { //sort method uses 'this' for array input
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);
}

private void doMergeSort(int lowerIndex, int higherIndex) { //indexed method for merge sort, states each step and case

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
doMergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}

private void mergeParts(int lowerIndex, int middle, int higherIndex) { //merge method using 'for' case,

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex; //declaring index variables for different cases
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) { //define loops for steps of different cases
System.out.print(" C "+i+" "+j);
ncompare=ncompare+1;
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}

C 0 1C 2 3C 0 2C 1 2C 1 3

这些是我现有算法中进行的前五次比较。我想阻止它比较 0 和 2,然后比较 1 和 2,因为用户可能会记住 2,就像比较 1 和 2,然后比较 1 和 3 时用户会记住 1 一样。

我想让比较的数字尽可能不让用户记住,以防止他们进行有偏见的比较

最佳答案

比较的顺序由合并排序算法定义。如果您以不可预测或“难以内存”的顺序比较索引,则您不再进行合并排序。您实际上必须发明一种新的排序算法才能实现这种行为。

但是,您可以在正常执行后以随机顺序打印比较结果。只需将它们存储在列表字段中,然后在排序后打乱并打印它们。例如:

public List<int[]> comparisons = new ArrayList<>();

mergeParts 中的打印替换为:

comparisons.add(new int[] { i, j });

排序后:

mms.sort(inputArr);
Collections.shuffle(mms.comparisons);
for (int[] c : mms.comparisons) {
System.out.print(" C "+c[0]+" "+c[1]);
}

关于java - 目的是防止主体/用户从内存中学习,你明白那是什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com