0

I need to check if an array is a sub-array of another bigger array. (find an word(sub-array) in a sentence(array)).

I need to do that in a recursion algorithm. that the run time will be log(n).

the arrays :

        char[] sentence = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};
    char[] word = {'l', 'l', 'o', 'w', 'o', 'r'};
                char [] word = {'t', 'n' , 'p'};

my code:

    static boolean wordFinder(char[] arr, char[] arr2, int l, int i) {

    if (i == arr2.length - 1) {
        return true;
    }
    if (arr[l] == arr2[i]) {
        return wordFinder(arr, arr2, l + 1, i + 1);
    }
    if (l == arr.length - 1) {
        return false;
    }
    return wordFinder(arr, arr2, l + 1, 0);


}

the third array is only for checking the code. (the code works, just need to know the run time).

Sufferring
  • 49
  • 5
  • It is totally unclear for me what is the problem here. What do you mean by "run time"? What does it have to do with those arrays? – Amongalen Jun 11 '19 at 10:19
  • I guess he means **complexity** by "run time". – MD Ruhul Amin Jun 11 '19 at 10:20
  • @Amongalen i need to check if the second array (char[] word) can be fitted to the first array (char [] sentence). i mean if the sentence is "hello world", and the word is "world". i can fit the word array to the sentence array. implement the word array in the sentence array. and by Run-time I mean . I know there are run time of O(n^2), O(n) , log(n) that symbols the run-time. i want to know what my run time is. O(n) or log(n). – Sufferring Jun 11 '19 at 10:21
  • And you say that your code works so what is the problem? – Amongalen Jun 11 '19 at 10:22
  • These question have been answered [here](https://stackoverflow.com/questions/5204051/how-to-calculate-the-running-time-of-my-program) – Raymond Toh Jun 11 '19 at 10:24
  • Are you actually asking that "is your current implementation complexity is O(log(n)) or other?" – MD Ruhul Amin Jun 11 '19 at 10:24
  • @Amongalen i want to know the run time. O(n) or log(n). – Sufferring Jun 11 '19 at 10:26
  • @ruhul yes. i am a student and my teacher gave me that as a homework. – Sufferring Jun 11 '19 at 10:27

2 Answers2

0

It sounds like you are interested in testing how long a block of code takes to run. To do that, just record the time before and after in milliseconds, and then subtract the two.

long start_time = System.currentTimeMillis();

...
...

long end_time = System.currentTimeMillis();

System.out.println("Code completed in " + (end_time - start_time) + "milliseconds.");

There's also System.nanoTime() for nanoseconds.

MadManCam
  • 1
  • 1
0

The runtime complexity of your program is O(n*m). Here n is the length or arr and m is the length of arr2 why: because in each recursion, there is actually nested loop in each recursion. One loop to iterate over arr and other is to iterate over arr2 for each element of arr.

Well, minimum complexity would be O(n).

Update: Have a look at this link: check-string-substring-another

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37