Does my pseudocode make sense? [duplicate]


i.stack.imgur.com/tOfrn.png

I believe my code is now foolproof. I will write up the pseudocode now. But I do have one question. Why does DRJava ask that I return something outside of my if statements? As you can see I wrote for ex: "return 1;" just because it asked. It will never return that value however. Can someone explain this to me?

public class assignment1question2test {
  public static void main(String[] args) {

    int[] a = new int[1];
    int l = 0;
    int r = a.length-1;

    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 = (r-l)/3 + l;
    int third2 = third1*2 - l + 1;

      if (r-l == 0) {
        return l;
      }

      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(r-l == 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<=l; g++) {
      count2 = count2 + a[g];
    }

        if (count1 == count2) {
          return 0;
        }
        if (count1 > count2) {
          return 1;
        }
        if (count1 < count2) {
          return -1;
        }  

        return 0;
}
}

FINAL PSEUDOCODE I THINK

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)/3
int third2 ← third1*2 - l + 1
if (r-l = 0) then
    return l
int compareResult  ← compare(a,l,third1,third1 + 1,third2)
if (r-l  = 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)

Answers:


String compareResult ← compare(a,l,mid,mid,r)

Here you check the middle element twice, make it:

String compareResult ← compare(a,l,mid,mid+1,r) Apart from that your algorithm seems fair enough to me.