I was studying about naive pattern search algorithm and found that it requires two loops to match the pattern present in a string or not. That time an Idea stuck in my mind and I think it would be a better approach because I used only one loop to find out the pattern in string. Here is
MY-PAT-ALGO(T[n],P[m]) [Here, T[n] is text string of length n and P[m] is pattern string of length m] 1. j <-- 0 , flag <-- 0 2. for i<-- 0 to n do if(T[i] = P[j]) j<-- j+1 else i<-- i-j j<-- 0 end else-if if(j = m) flag <-- 1 Print: "Pattern matched" break end if end for loop 3. if(flag = 0) Print: "Pattern not found" 4. Exit.
I want to know why it could be worst in comparison of naive approach. According to my test cases I found loop worst case is better than naive approach loop.