Monday, 20 January 2014

Linear Search (searching through a text file)

The linear search is a very simple searching algorithm. The way it works is that it starts from the beginning of an Array or List<T> and goes through checking whether the given value or object exists in a data structure.

For example, a linear search is very similar to looking in a phonebook, and you start with section 'A' and look and look until you find the exact number you're looking for. The Linear Search has O(n) time complexity, so its recommended to use with smaller collections rather than large collections. With larger collections a binary search is more efficient, which will be the topic of my next post.

The following code is a linear search made to search a text file for a certain term the user enters.

class Program
    {
        static void Main()
        {

            DisplayTweets();//this method is called so that when the application loads, the lines of text get displayed automatically.
            //the method that is called above is important because if the file has been opened in another part of the program, we can open it in this part and search and write tweets.
            //if I load once in Main, I cannot load the file again in this part of the program. There is a way around it, but for now, calling this method is simple.


            // File.OpenText allows us to read the contents of a file by establishing
            // a connection to a file stream associated with the file.
            StreamReader reader = File.OpenText("textfile.txt");//reading from "tweets.txt" in bin/debug
            Console.WriteLine();

            // We can now read data from the file using ReadLine.
            String searchingLine = reader.ReadLine();
           


          
                Console.Write("\n\nEnter a search term to display all lines containing the term: ");
//prompt the user to enter a search term to look for in the text file
                string searchTerm = Console.ReadLine();

                while (searchingLine != null)
                {
                    // We can use String.Split to separate a line of data into fields.

                    String[] lineArray = searchingLine.Split(' ');
//we assign a string array for each line read in the text file so that when we read in a line, its stored in an array
                   
                    for (int x = 0; x < lineArray.Length; x++)//because name is already stored at the first index (0), that can only mean that the entire tweet is stored
                    {
//therefore, a for loop has to be used to go through the entire line, and check, if any index in lineArray //== the search term
                        if (lineArray[x] == searchTerm)
                        {
                            Console.WriteLine();//adding a writeline here forces every line of text to be printed on a new line
                            for (int i = 0; i < searchingLine.Length; i++)
                            {//go though the searchingLine and write out the entire tweet.
                                Console.Write("{0}", searchingLine[i].ToString());
                            }

                        }
                    }
                    searchingLine = reader.ReadLine();
                }
                       
           
            Console.ReadLine();//keep the window open
}





static void DisplayTweets()//this method will do one thing only: display all the text in the file
        {
            //this is only a simple function, it does not return anything, so it is void
            StreamReader reader = File.OpenText("textfile.txt");
            String line = reader.ReadLine();
            using (StreamReader alternateReader = File.OpenText("textfile.txt"))
            {
                line = alternateReader.ReadLine();

                while (line != null)
                {
                    Console.WriteLine("{0}", line);

                    line = alternateReader.ReadLine();
                }
            }
        }

No comments:

Post a Comment