Beispiel #1
0
        //**********************************************************************************************
        /// <summary>
        /// This method will search the given list of records for matching days and return a match or a 'did not match' result in a list
        /// </summary>
        //public enum DayOfWeek

        public List <StoredData> searchByDayA2(List <StoredData> dataToSearchA2, string dayToFind)
        {
            int countOfRepetitions = 0; // use this to hold the number of repetitions your algorithm has to make to search the data
            //#####################################################################################################
            //Place your second search algorithm below here - the results must be in the List 'searchResults'
            //This will be displayed on the right-hand-side of the console window
            //#####################################################################################################
            //Create a blank data item to indicate the searched for date doesnot exist in the data set
            List <StoredData> searchResults = new List <StoredData>();

            //Binary search
            //Worst and Average case: O(log n)
            //Best case: O(1)

            List <int> numberList = new List <int>();

            for (int i = 0; i <= dataToSearchA2.Count - 1; i++)
            {
                numberList.Add(indexDay(dataToSearchA2[i].TxDay));  //converts the list of days of the week to a new list with numbers 1 - 7 to represent days of the week
            }

            numberList.Sort();                                                               //sorts the new number list into numerical order

            dataToSearchA2.Sort((x, y) => dayNumber(x.TxDay).CompareTo(dayNumber(y.TxDay))); //compares it to number value for numerical order sorting

            int lowBound = 0, highBound = numberList.Count - 1;

            int mid = 0;

            int N = dataToSearchA2.Count;

            int numberDay = dayNumber(dayToFind);//returns number from the day of the week entered


            while (lowBound <= highBound)
            {
                mid = (lowBound + highBound) / 2;
                if (numberList[mid] < numberDay)//the element we search is located to the right from the mid point
                {
                    lowBound = mid + 1;
                }
                else if (numberList[mid] > numberDay)//the element we search is located to the left from the mid point
                {
                    highBound = mid - 1;
                }

                //at this point low and high bound are equal and we have found the element or
                //list[mid] is just equal to the value => we have found the searched element
                else if (numberList[mid] == numberDay)
                {
                    searchResults.Add(dataToSearchA2[mid]);


                    int plusMid  = mid;
                    int minusMid = mid;


                    if (plusMid < dataToSearchA2.Count - 1)
                    {
                        plusMid = plusMid + 1;
                    }

                    if (minusMid > 0)
                    {
                        minusMid = minusMid - 1;
                    }


                    if (numberList[0] == numberDay)
                    {
                        searchResults.Add(dataToSearchA2[0]);
                    }

                    while ((numberList[plusMid] == numberDay) && (plusMid < dataToSearchA2.Count - 1)) //checks the index to the right to find all the matching values

                    {
                        searchResults.Add(dataToSearchA2[plusMid]);//adds the element to the right if it matches the search element
                        plusMid = plusMid + 1;

                        countOfRepetitions++;
                    }

                    while ((numberList[minusMid] == numberDay) && (minusMid > 0)) //checks the index to the left to find all the matching values

                    {
                        searchResults.Add(dataToSearchA2[minusMid]);//adds the element to the left if it matches the search element
                        minusMid = minusMid - 1;

                        countOfRepetitions++;
                    }


                    //Console.WriteLine(numberList[mid] + "  this is the index:  " + mid + "       " + numberDay);

                    break;
                }//end of else for when the correct value has been found

                countOfRepetitions++;
            }//end of while loop

            if (lowBound > highBound)

            {
                StoredData noValue = new StoredData("Not Found", DateTime.Now, 0, 0, 0, 0);//if the element can't be found an error is shown
                searchResults.Add(noValue);
            }


            //#####################################################################################################
            //Place your second search algorithm below here - the results must be in the List 'searchResults'
            //#####################################################################################################
            // *** your search result data should be placed in 'searchResults'
            searchResults.First().SearchTypeAndTime = "Using 'Binary Search A2' search by day"; //rewrite the text to show user which algorithm you have used
            // place your total count of loops (countOfRepetitions) here
            searchResults.First().CountRepetitions = countOfRepetitions;                        //rewrite to show user the number of steps (loops) this algorithm has made to sort the data

            return(searchResults);
        }
Beispiel #2
0
        //---------------------------------------------------------------------------------------------
        #region search date
        /// <summary>
        /// This method will search the given list of records for matching dates and return a match or a 'did not match' result in a list
        /// </summary>
        public List <StoredData> searchByDateA1(List <StoredData> sortedListToSearchA1, DateTime dateToFind)
        {
            int countOfRepetitions = 0; // use this to hold the number of repetitions your algorithm has to make to search the data
            //#####################################################################################################
            //Place your first search algorithm below here - the results must be in the List 'searchResults'
            //This will be displayed on the left-hand-side of the console window
            //#####################################################################################################
            List <StoredData> searchResults = new List <StoredData>();

            //Interpolation Search
            //Average Case: log(log(n))
            //Worst Case: O(n)

            sortedListToSearchA1.Sort((x, y) => x.TxDate.CompareTo(y.TxDate)); //sorts dates in sequential order

            int low = 0;
            int mid;
            int high = sortedListToSearchA1.Count - 1;

            while (sortedListToSearchA1[low].TxDate < dateToFind && sortedListToSearchA1[high].TxDate >= dateToFind)  //the search space
            {
                mid = low + ((high - low) / 2);
                if (sortedListToSearchA1[mid].TxDate < dateToFind)  //search space is reduced to the part before
                {
                    low = mid + 1;
                }
                else if (sortedListToSearchA1[mid].TxDate > dateToFind)
                {
                    high = mid - 1;
                }

                else
                {
                    searchResults.Add(sortedListToSearchA1[mid]); //adds current iteration of mid index in sortedListToSearchA1
                    break;
                }

                countOfRepetitions++;
            }

            if (sortedListToSearchA1[low].TxDate == dateToFind) //in case the search date is located at lowest index in list
            {
                searchResults.Add(sortedListToSearchA1[low]);
            }

            else if (searchResults.Count == 0) //if the date can't be found error is added to search results
            {
                StoredData newObj = new StoredData("Not Found", DateTime.Now, 0, 0, 0, 0);
                searchResults.Add(newObj);
            }

            // counts iterations of how many time the search passes through the list.
            //#####################################################################################################
            //Place your first search algorithm above here - the results must be in the List 'searchResults'
            //#####################################################################################################
            // *** your search result data should be placed in 'searchResults'

            searchResults.First().SearchTypeAndTime = "Using 'Interpolation Search A1' search by date"; //rewrite the text to show user which algorithm you have used
                                                                                                        // place your total count of loops (countOfRepetitions) here
            searchResults.First().CountRepetitions = countOfRepetitions;
            //rewrite to show user the number of steps (loops) this algorithm has made to sort the data

            return(searchResults);
        }