开发者

Recursive Linear Search....help me find mistakes

I'm trying to write a class with a recursive Linear search method. This is what I have so far. Can you please help me out with figuring where I'm going wrong? Please :)

I was asked specifically for a method public int linearSearchRecursive(Tile value) and method addTile(Tile tile) since the parameter is Tile value. I do not know how to do that. Please point me in the right diretion with the tile.

class LinearSearch {
    public static int LinearSearch(int a[], int value) {
        return LinearSearch(value);
    }
    private int[] numbers;

    public int LinearSearch(int size) {
        numbers = new int[size];
    }


    public int linearSearchRecursive(Tile value) {

        if (value == numbers[startingIndex]) {
            return startingIndex;
        }

        else if (startingIndex + 1 < numbers.length) {
            return linearSearch开发者_如何学Go(value, startingIndex + 1);
        }

        else {
            return -1;
        }

    }
}


Here are some tips :

  • For your method to be considered recursive, it must call itself. You aren't doing that right now. You may think you are but you should look a bit closer at your method declarations.
  • You should probably name your search method so that it's not easily confused with the constructor.
  • Your recursive method needs a base case so it can terminate.


Your code is not compiled, i think you want to write your code in some kind of pseudo code.

Firstly , i recommend you to read Recursive Algorithm Section of a data structure book,which is depend on your choise.

You know the complexity of Linear Search is O(n), because your list could be unsorted and for this reason you should look every item of this list.

int linearSearchIterative(int[] input, int key) {
        for (int i = 0; i < input.length; i++) {
            if (input[i] == key) {
                return i;
            }
            else continue;
        }

        return -1;
    }

As simple linear search algorithms is like above code section.

How can we transform this algorithm from iterative to recursive? You should focus on and resolve this problem firstly.

When i looked your code there are small mistakes. These mistakes

  1. type of using constructor

  2. recreation of int array (this array is the array in which you search the given item, so it has no sense)

  3. i cant work out the relationship between LinearSearch constructure like method and linearSearchRecursive.

When we dont think your code stucture we can see that your code works with error depends on logical mistake. You firstly control the index parameter, if index == size of array you should return -1; after so- called control, you should check whether the item in given index is equal the key or not. if it is not equal call your recursive function again.

so algorithm is like this piece of code

private int linearSearchRecursive(int[] input, int key,int index) {
        if (index == input.length-1) {
            return -1;
        }
        if (input[index] == key) {
            return index;
        }
        else 
        return linearSearchRecursive(input,key,++index);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜