-1

I need to count the number of comparisons done by the following search function.. Also how to calculate the execution time..Any help? I want the count and time to be printed in the output.

// C# program for Naive Pattern Searching 
using System; 

class GFG { 

    public static void search(String txt, String pat) 
    { 
        int M = pat.Length; 
        int N = txt.Length; 

        /* A loop to slide pat one by one */
        for (int i = 0; i <= N - M; i++) { 
            int j; 

            /* For current index i, check for pattern 
            match */
            for (j = 0; j < M; j++) 
                if (txt[i + j] != pat[j]) 
                    break; 

            // if pat[0...M-1] = txt[i, i+1, ...i+M-1] 
            if (j == M) 
                Console.WriteLine("Pattern found at index " + i); 
        } 
    } 

    // Driver code 
    public static void Main() 
    { 
        String txt = "AABAACAADAABAAABAA"; 
        String pat = "AABA"; 
        search(txt, pat); 
    } 
} 
// This code is Contributed by Sam007 
Memo
  • 5
  • 4
  • _This code is Contributed by Sam007_ Does it mean that this code was written by someone else and now you are looking for finishing it by others? Have you tried to debug it and accomplish something to count and measure the execution time? – Pavel Anikhouski Apr 18 '20 at 18:12
  • Please show what you have tried so far. Stopwatch is for timing with good precision. Does "Number of comparisons" mean every `!=` or `==` ? – Oguz Ozgul Apr 18 '20 at 18:17
  • @PavelAnikhouski this code is published online and I am in level of learning the algorithms so that I want to understand other relative concepts – Memo Apr 18 '20 at 18:40

1 Answers1

0

I need to count the number of comparisons done by the following search function...

You can use counter for this:

public static void search(String txt, String pat, out int counter)
{
    int M = pat.Length;
    int N = txt.Length;

    counter = 0;

    /* A loop to slide pat one by one */
    for (int i = 0; i <= N - M; i++)
    {
        int j;

        /* For current index i, check for pattern 
        match */
        for (j = 0; j < M; j++)
        {
            counter++; // counter for below if statement

            if (txt[i + j] != pat[j])
            {
                break;
            }
        }

        // if pat[0...M-1] = txt[i, i+1, ...i+M-1] 
        if (j == M)
        {
            Console.WriteLine("Pattern found at index " + i);
        }

        counter++; // counter for above if statement
    }
}

Also how to calculate the execution time.

You can use StopWatch class for this case:

public static async Task Main(string[] args)
{
    String txt = "AABAACAADAABAAABAA";
    String pat = "AABA";

    var stopWatch = new Stopwatch();
    stopWatch.Start();
    search(txt, pat, out var counter);
    stopWatch.Stop();

    Console.WriteLine("--------------");
    Console.WriteLine($"Count of operations: {counter}, elapsed time: {stopWatch.ElapsedMilliseconds} miliseconds");
}
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
  • I tried this but is not working with me..Can you please show me the whole code including the code I posted in the begining? – Memo Apr 18 '20 at 19:39