你能快点告诉我这个伪代码是否有意义吗?

在此处输入图像描述

我相信我的代码现在是万无一失的。 我现在会写出伪代码。 但我确实有一个问题。 为什么DRJava要求我在if语句之外返回一些东西? 正如你所看到我为前写的:“返回1;” 只是因为它问。 但它永远不会返回那个值。 谁可以给我解释一下这个?

public class assignment1question2test { public static void main(String[] args) { int[] a = new int[50]; int l = 0; int r = a.length; for(int i=0; i<r; i++) { a[i] = 1; } a[0] = 10; for (int i=0; i<r; i++) { System.out.println(a[i]); } System.out.print(recursiveSearch(a,l,r)); } public static int recursiveSearch (int[] a, int l, int r) { int third1 = (rl)/3 + l; int third2 = third1*2 - l + 1; System.out.println("i will be checking compare from " + l + " to " + third1 + " and " + (third1 + 1) + " to " + third2); int compareResult = compare(a,l,third1,third1 + 1, third2); if(rl == 1) { if (compareResult == 1) { return l; } else { return r; } } if (compareResult == 0) { return recursiveSearch(a,third2 + 1, r); } if (compareResult == 1) { return recursiveSearch(a,l,third1); } if (compareResult == -1) { return recursiveSearch(a,third1 + 1, third2); } return 1; } public static int compare(int[] a, int i, int j, int k, int l) { int count1 = 0; int count2 = 0; for(int g=i; g<=j; g++) { count1 = count1 + a[g]; } for(int g=k; g count2) { return 1; } if (count1 < count2) { return -1; } return 0; } } 

更新的最终伪代码:

 Algorithm: recursiveSearch (a,l,r) Inputs: An array a, indices l and r which delimit the part of interest. Output: The index that has the lead coin. int third1 ← (r - l + 1)/3 int third2 ← third1*2 - l + 1 if (rl = 0) then return l int compareResult ← compare(a,l,third1,third1 + 1,third2) if (rl = 1) then if (compareResult = 1) then return l else return r if (compareResult = 0) then return recursiveSearch(a, third2 + 1, r) if (compareResult = "1") then return recursiveSearch(a,l,third1) if (compareResult = "-1") then return recursiveSearch(a,third1 + 1,third2) 

无论哪一方面较大,您似乎都会在以下搜索中包含mid值。 递归调用应该从搜索空间中排除mid

而且,为了使比较有意义,被比较的两个组需要具有相同的大小。 这将需要一些额外的奇数/偶数逻辑。