//Функция для высчитывания новой точки
        Coord newPoint(Coord x1, Coord x2, opt opt_A)
        {
            Matrix E    = Matrix.E(amountVar);
            Coord  grad = Coord.Gradient(y, x2, amountVar);

            if (k % amountVar == 1)
            {
                //if (k == 1)
                A = E;
            }
            else
            {
                A = opt_A(x1, x2);
            }
            p = -A * grad;
            p = p / p.Norma;
            LinearSearch f1 = new LinearSearch(x2, p, y);

            f1.Svenn(20);
            f1.Bolcano(10);
            f1.Davidon();
            alfa = f1.alfa_min;
            Coord x3 = x2 + alfa * p / p.Norma;

            return(x3);
        }
Ejemplo n.º 2
0
        public void SearchByLinear()
        {
            int index       = 1;
            int indexOfNine = LinearSearch.Search(intArray, 9);

            Assert.IsTrue(index.Equals(indexOfNine));
        }
Ejemplo n.º 3
0
        public void LinearSearchTest()
        {
            int number = 100;
            int index;
            int searchedElement;
            int searchedIndex;
            int[] arr = Get(number).ToArray();
            LinearSearch<int> search = new LinearSearch<int>(arr);
            Random rand = new Random();

            searchedIndex = arr.Length - 1;
            searchedElement = arr.Last();
            index = search.Search(searchedElement);
            Assert.AreEqual(searchedIndex, index);

            searchedIndex = 0;
            searchedElement = arr.First();
            index = search.Search(searchedElement);
            Assert.AreEqual(searchedIndex, index);

            searchedIndex = number - 1 - rand.Next(number - 1);
            searchedElement = arr[searchedIndex];
            index = search.Search(searchedElement);
            Assert.AreEqual(searchedIndex, index);
        }
        public void Start()
        {
            #region LinearSearch

            var ls = new LinearSearch();
            TestAlgorithm("Linear Search", ls.Solution);

            #endregion

            #region BinarySearch

            var bs = new BinarySearch();
            TestAlgorithm("Binary Search", bs.Solution);
            CalcLatency("Binary FindFirst", bs.FindFirst, new int[] { 0, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 6, 7, 8, 9 }, 5);
            CalcLatency("Binary FindLast", bs.FindLast, new int[] { 0, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 6, 7, 8, 9 }, 5);
            CalcLatency("Binary CountOcurrences", bs.CountRepetitions, new int[] { 0, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 6, 7, 8, 9 }, 5);

            #endregion

            #region JumpSearch

            var js = new JumpSearch();
            TestAlgorithm("Jump Search", js.Solution);

            #endregion

            #region InterpolationSearch

            var @is = new InterpolationSearch();
            TestAlgorithm("Interpolation Search", @is.Solution);

            #endregion
        }
Ejemplo n.º 5
0
    private void Start()
    {
        testCases.Add(new SearchTestCase(new int[] { 5, 7, 7, 8, 8, 10 }, 8));
        testCases.Add(new SearchTestCase(new int[] { 3, 3, 3 }, 3));
        testCases.Add(new SearchTestCase(new int[] { 1 }, 1));

        double ms   = 0;
        float  time = Time.realtimeSinceStartup;

        // linear search
        foreach (SearchTestCase testCase in testCases)
        {
            int[] result = LinearSearch.SearchArray(testCase.SortedArray, testCase.Target);
            Debug.Log(string.Format("{0}:{1}", result[0], result[1]));
        }

        ms = Math.Round((Time.realtimeSinceStartup - time) * MS_IN_SEC, 2);
        Debug.Log(string.Format("Linear: {0}ms", ms));
        time = Time.realtimeSinceStartup;

        // binary search
        foreach (SearchTestCase testCase in testCases)
        {
            int[] result = BinarySearch.SearchArray(testCase.SortedArray, testCase.Target);
            Debug.Log(string.Format("{0}:{1}", result[0], result[1]));
        }

        ms = Math.Round((Time.realtimeSinceStartup - time) * MS_IN_SEC, 2);
        Debug.Log(string.Format("Binary: {0}ms", ms));
        time = Time.realtimeSinceStartup;
    }
Ejemplo n.º 6
0
 public TestBase()
 {
     DataParameters = new DataParameters(ProblemConstants.InitialNoOfEntries);
     DataGenerator  = new DataGenerator(DataParameters);
     LinearSut      = new LinearSearch(DataGenerator);
     BinarySut      = new BinarySearch(DataGenerator);
 }
Ejemplo n.º 7
0
        public static void TestSearching()
        {
            int[] data = Enumerable.Range(1, 100000000).ToArray <int>();

            int dataToFind = 100000000;
            var watch      = new Stopwatch();

            watch.Start();
            var result = BinarySearch.Recursive(data, dataToFind, 0, data.Length - 1);

            Console.WriteLine(result);
            watch.Stop();
            Console.WriteLine($"Total Execution Time Binary Recursive: {watch.ElapsedMilliseconds} ms");


            if (!watch.IsRunning)
            {
                watch.Restart(); // Reset time to 0 and start measuring
            }
            watch.Start();
            result = BinarySearch.Iterative(data, dataToFind);
            Console.WriteLine(result);
            watch.Stop();
            Console.WriteLine($"Total Execution Time Binary Iterative: {watch.ElapsedMilliseconds} ms");

            if (!watch.IsRunning)
            {
                watch.Restart(); // Reset time to 0 and start measuring
            }
            watch.Start();
            result = LinearSearch.Normal(data, dataToFind);
            Console.WriteLine(result);
            watch.Stop();
            Console.WriteLine($"Total Execution Time Linear: {watch.ElapsedMilliseconds} ms");
        }
Ejemplo n.º 8
0
        public void SearchTest()
        {
            int[] arr = { 2, 5, 6, 70, 30, 20, 3, 1, 9 };
            LinearSearch <int> search = new LinearSearch <int>(arr);

            Assert.AreEqual(search.Search(1), 7);
            Assert.AreEqual(search.Search(0), -1);
            Assert.AreEqual(search.Search(80), -1);
        }
Ejemplo n.º 9
0
    public static int LastIndexOf <T>(this IList <T> source, Func <T, bool> predicate)
    {
        ArgumentNullException.ThrowIfNull(source);

        const int minIndex = 0;
        var       maxIndex = source.Count - 1;

        return(LinearSearch.LastIndexOf(minIndex, maxIndex, index => predicate(source[index])));
    }
Ejemplo n.º 10
0
        public void TestWhenItemDoesNotExistItReturnMinusOne()
        {
            var tmp      = new LinearSearch <int>();
            var data     = new int[] { 1, 5, 2, 9, 6, 6, 4, 2 };
            var actual   = tmp.Search(data, -1);
            var expected = -1;

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 11
0
    public static int LastIndexOf <T>(this IList <T> source, Func <T, bool> predicate)
    {
        Assert.IsNotNull(source);

        const int minIndex = 0;
        var       maxIndex = source.Count - 1;

        return(LinearSearch.LastIndexOf(minIndex, maxIndex, index => predicate(source[index])));
    }
Ejemplo n.º 12
0
        public void TestItReturnMinusOneWhenArrayIsNull()
        {
            var tmp = new LinearSearch <int>();

            var actual   = tmp.Search(null, -1);
            var expected = -1;

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 13
0
        public void SearchTest(int[] array, int toFind, int index)
        {
            // Arrange
            // Act
            var result = LinearSearch.Search(array, toFind);

            // Assert
            Assert.Equal(index, result);
        }
Ejemplo n.º 14
0
        public void TestItemIsFoundIfItExists()
        {
            var tmp      = new LinearSearch <int>();
            var data     = new int[] { 1, 5, 2, 9, 6, 6, 4, 2 };
            var actual   = tmp.Search(data, 1);
            var expected = 0;

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 15
0
        public void TestItReturnMinusOneWhenArrayIsEmpty()
        {
            var tmp      = new LinearSearch <int>();
            var data     = new int[] { };
            var actual   = tmp.Search(data, -1);
            var expected = -1;

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 16
0
        public static void Main(string[] args)
        {
            Point[]   points    = null;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            LoadCities(out points);
            stopwatch.Stop();
            Console.WriteLine("Loading of cities.txt took: " + stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            LinearSearch <Point> linearSearch = new LinearSearch <Point>();

            linearSearch.Create(points, CalculatePointDistance);
            stopwatch.Stop();
            Console.WriteLine("Creation of linear search took: " + stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            VpTree <Point> vpTree = new VpTree <Point>();

            vpTree.Create(points, CalculatePointDistance);
            stopwatch.Stop();
            Console.WriteLine("Creation of VP tree search took: " + stopwatch.Elapsed);

            Point[] resultsLinear = null;
            Point[] resultsVpTree = null;

            double[] distancesLinear = null;
            double[] distancesVpTree = null;

            Point ourtarget = new Point();

            ourtarget.latitude  = 43.466438;            // Use same target as Steve Hanov did
            ourtarget.longitude = -80.519185;

            stopwatch.Reset();
            stopwatch.Start();
            linearSearch.Search(ourtarget, 8, out resultsLinear, out distancesLinear);
            stopwatch.Stop();
            Console.WriteLine("Linear search took: " + stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            vpTree.Search(ourtarget, 8, out resultsVpTree, out distancesVpTree);
            stopwatch.Stop();
            Console.WriteLine("VP tree search took: " + stopwatch.Elapsed);

            Console.WriteLine("RESULTS:");
            for (int i = 0; i < resultsVpTree.Length; i++)
            {
                Console.WriteLine(resultsVpTree[i].city);
                Console.WriteLine(" " + distancesVpTree[i]);
            }
        }
Ejemplo n.º 17
0
        public void SearchSingleLinearTest()
        {
            string path  = @"../../words.txt";
            var    lines = File.ReadLines(path).ToList();

            LinearSearch search = new LinearSearch();

            search.SearchLinear("AAA", lines);
            Assert.IsNotNull(search);
        }
Ejemplo n.º 18
0
        public void pxWithinAndOutside()
        {
            var vdSearch = new VectorDominationSearch();
            var lSearch  = new LinearSearch();

            vdSearch.Run(points3, window);
            lSearch.Run(points3, window);

            Assert.AreEqual(lSearch.searchedPoins.Count, vdSearch.searchedPoins.Count);
        }
Ejemplo n.º 19
0
        public void CheckBorderWithCorners()
        {
            var vdSearch = new VectorDominationSearch();
            var lSearch  = new LinearSearch();

            vdSearch.Run(borderCornerPoints, window);
            lSearch.Run(borderCornerPoints, window);

            Assert.AreEqual(lSearch.searchedPoins.Count, vdSearch.searchedPoins.Count);
        }
        public void Test1()
        {
            int x = 50;

            int[]        array        = new int[] { 10, 20, 30, 40, 50, 60, 70 };
            LinearSearch linearSearch = new LinearSearch();
            var          ret          = linearSearch.Search(array, x);

            Assert.Equals(ret, Array.IndexOf(array, x));
        }
Ejemplo n.º 21
0
    public void TestLinearSearch()
    {
        int[] numbers = { 1, 2, 3, 4, 5 };

        Assert.True(LinearSearch.contains(numbers, 3));
        Assert.True(LinearSearch.contains(numbers, 1));
        Assert.True(LinearSearch.contains(numbers, 5));

        Assert.False(LinearSearch.contains(numbers, 6));
    }
Ejemplo n.º 22
0
        public void CheckEqualPoints()
        {
            var vdSearch = new VectorDominationSearch();
            var lSearch  = new LinearSearch();

            vdSearch.Run(equalPoints, window);
            lSearch.Run(equalPoints, window);

            Assert.AreEqual(lSearch.searchedPoins.Count, vdSearch.searchedPoins.Count);
        }
Ejemplo n.º 23
0
        public static void Main()
        {
            var elements = Console.ReadLine()
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(int.Parse)
                           .ToArray();

            var targetElement = int.Parse(Console.ReadLine());

            Console.WriteLine(LinearSearch <int> .Search(elements, targetElement, 0));
        }
 public void Search_DistinctElements_ExpectsToSuccessfullyGetTheIndexOfTheirPosition()
 {
     Assert.AreEqual(4, LinearSearch.Search(_list, 3, _startIndex, _endIndex));
     Assert.AreEqual(3, LinearSearch.Search(_list, 10, _startIndex, _endIndex));
     Assert.AreEqual(7, LinearSearch.Search(_list, 14, _startIndex, _endIndex));
     Assert.AreEqual(6, LinearSearch.Search(_list, 25, _startIndex, _endIndex));
     Assert.AreEqual(0, LinearSearch.Search(_list, 27, _startIndex, _endIndex));
     Assert.AreEqual(9, LinearSearch.Search(_list, 34, _startIndex, _endIndex));
     Assert.AreEqual(11, LinearSearch.Search(_list, 78, _startIndex, _endIndex));
     Assert.AreEqual(2, LinearSearch.Search(_list, 120, _startIndex, _endIndex));
 }
Ejemplo n.º 25
0
        public static void Main(string[] args)
        {
            Point[] points = null;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            LoadCities(out points);
            stopwatch.Stop();
            Console.WriteLine("Loading of cities.txt took: " + stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            LinearSearch<Point> linearSearch = new LinearSearch<Point>();
            linearSearch.Create(points, CalculatePointDistance);
            stopwatch.Stop();
            Console.WriteLine("Creation of linear search took: " + stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            VpTree<Point> vpTree = new VpTree<Point>();
            vpTree.Create(points, CalculatePointDistance);
            stopwatch.Stop();
            Console.WriteLine("Creation of VP tree search took: " + stopwatch.Elapsed);

            Point[] resultsLinear = null;
            Point[] resultsVpTree = null;

            double[] distancesLinear = null;
            double[] distancesVpTree = null;

            Point ourtarget = new Point();
            ourtarget.latitude = 43.466438; // Use same target as Steve Hanov did
            ourtarget.longitude = -80.519185;

            stopwatch.Reset();
            stopwatch.Start();
            linearSearch.Search(ourtarget, 8, out resultsLinear, out distancesLinear);
            stopwatch.Stop();
            Console.WriteLine("Linear search took: " + stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            vpTree.Search(ourtarget, 8, out resultsVpTree, out distancesVpTree);
            stopwatch.Stop();
            Console.WriteLine("VP tree search took: " + stopwatch.Elapsed);

            Console.WriteLine("RESULTS:");
            for (int i = 0; i < resultsVpTree.Length; i++)
            {
                Console.WriteLine(resultsVpTree[i].city);
                Console.WriteLine(" " + distancesVpTree[i]);
            }
        }
Ejemplo n.º 26
0
        public void LinearSearch_Find_FindMiddle()
        {
            var search = new LinearSearch();

            var list = new List <int>()
            {
                2, 4, 6
            };
            var result = search.Find(list, 4);

            Assert.AreEqual(1, result.PositionFound);
        }
Ejemplo n.º 27
0
        public void LinearSearch_Find_NoFind()
        {
            var search = new LinearSearch();

            var list = new List <int>()
            {
                2, 4, 6
            };
            var result = search.Find(list, 5);

            Assert.IsNull(result.PositionFound);
        }
Ejemplo n.º 28
0
        //Zusatzaufgabe 2 - nicht abgeschlossen
        public static void TaskC()
        {
            _Rechnung[] re = LinearSearch.ErzeugeZufallsRechnung2(),
                re2 = new _Rechnung[re.Length];

            //Array kopieren
            for (int i = 0; i < re.Length; ++i)
                re2[i] = re[1];

            BubbleSortRechnungen(re, true);

            Console.WriteLine("BubbleSort wurde erfolgreich ausgeführt");
        }
Ejemplo n.º 29
0
        public void LinearSearchTest()
        {
            LinearSearch <int> linearSearch = new LinearSearch <int>();


            for (int i = 0; i < 100; i++)
            {
                FillRandom();
                linearSearch.Items.Clear();
                linearSearch.Items.AddRange(this.Items);
                Assert.AreEqual(this.Items.IndexOf(this.SearchItem), linearSearch.ToFind(this.SearchItem));
            }
        }
        public void LinearSearch_ItemExists_ReturnIndex()
        {
            //Arrange
            LinearSearch aLinearSearch = new LinearSearch();


            //Act
            var index = aLinearSearch.Linear_Search(new[] { 1, 2, 3, 4, 5, 6, 7 }, 7, 2);


            //Assert
            Assert.AreEqual(index, 1);
        }
Ejemplo n.º 31
0
        public void LinearSearch_NumberFound_Two()
        {
            // Arrange
            int[] data           = new int[] { 1, 2, 3, 4, 5 };
            int   searchNumber   = 3;
            int   result         = 0;
            int   expectedResult = 2;

            // Act
            result = LinearSearch.Search(data, searchNumber);

            // Assert
            Assert.AreEqual(result, expectedResult);
        }
Ejemplo n.º 32
0
        public void LinearSearch_NumberNotFound_MinusOne()
        {
            // Arrange
            int[] data           = new int[] { 1, 2, 3, 4, 5 };
            int   searchNumber   = 10;
            int   result         = 0;
            int   expectedResult = -1;

            // Act
            result = LinearSearch.Search(data, searchNumber);

            // Assert
            Assert.AreEqual(result, expectedResult);
        }
Ejemplo n.º 33
0
        public static void Main(string[] args)
        {
            Point[] points = null;

            LoadCities(out points);

            LinearSearch<Point> linearSearch = new LinearSearch<Point>();
            linearSearch.Create(points, CalculatePointDistance);

            VpTree<Point> vpTree = new VpTree<Point>();
            vpTree.Create(points, CalculatePointDistance);

            Point[] resultsLinear = null;
            Point[] resultsVpTree = null;

            double[] distancesLinear = null;
            double[] distancesVpTree = null;

            System.Random rand = new System.Random();
            Point ourtarget;

            for (int i = 0; i < 100; i++)
            {
                ourtarget = new Point("random", rand.NextDouble() * 180 - 90, rand.NextDouble() * 180 - 90);

                linearSearch.Search(ourtarget, 5+i/10, out resultsLinear, out distancesLinear);

                vpTree.Search(ourtarget, 5+i/10, out resultsVpTree, out distancesVpTree);

                CompareResults(resultsLinear, resultsVpTree);
            }

            if (errorCount == 0)
            {
                Console.WriteLine("TEST PASSED!");
            }
            else
            {
                Console.WriteLine("TEST FAILED!");
            }
        }