Tuesday, March 19, 2013

Linear Search in array

// Linear Search
using
System;

public
class LinearSearcher{
    public static void Main()
    {
        String[] myArray = new String[7] { "kama", "dama", "lama", "yama", "pama", "rama", "lama" };
        String myString = "lama";
        Int32 myIndex;

        // Search for the first occurrence of the duplicated value in a section of the        myIndex = Array.IndexOf(myArray, myString, 0, 6);
        Console.WriteLine("The first occurrence of \"{0}\" between index 0 and index 6 is at index {1}.", myString, myIndex);

        // Search for the last occurrence of the duplicated value in a section of the       myIndex = Array.LastIndexOf(myArray, myString, 6, 7);
        Console.WriteLine("The last occurrence of \"{0}\" between index 0 and index 6 is at index {1}.", myString, myIndex);
        Console.ReadLine();
    }
}

No comments:

Post a Comment