예제 #1
0
 static void Main(string[] args)
 {
     //lesson one
     Console.WriteLine(CreateHeader("Sequential search"));
     Console.WriteLine(SequentialSearch.Sequentialsearch("Tim"));
     Console.WriteLine(CreateHeader("Binary search"));
     Console.WriteLine(BinarySearch.Binarysearch(25));
     //lesson two
     Console.WriteLine(CreateHeader("Insertion sort"));
     Console.WriteLine(Insertionsort.InsertionSort());
     Console.WriteLine(CreateHeader("Bubble sort"));
     Console.WriteLine(Bubblesort.BubbleSort());
     Console.WriteLine(CreateHeader("Merge sort"));
     Console.WriteLine(Mergesort.MergeSort());
     Console.ReadLine();
 }
        //O(nLogn) time complexity due to sorting
        static int CountPairsWithDiffK(int[] arr, int k)
        {
            int count = 0;

            //sort the array first
            Array.Sort(arr); //O(nLogn)

            for (int i = 0; i < arr.Length; i++)
            {
                if (BinarySearch.RecursiveBinarySearch(arr, i + 1, arr.Length - 1, arr[i] + k) != -1)
                {
                    count++;
                }
            }

            return(count);
        }
예제 #3
0
        static void Main(string[] args)
        {
            int[] arr = { 1, 10, 30, 15, 23, 44, 62, 1, 4, 8, 6, -6 };
            int   x   = 30;

            Console.WriteLine("Given Number : " + x);


            //LINEAR SEARCH
            Console.WriteLine("Linear Search Algorithm Result : " + LinearSearch.search(arr, x) + " are greater than " + x);


            //BINARY SEARCH
            Console.WriteLine("Binary Search Algorithm Result : " + BinarySearch.binarySearch(arr.OrderBy(y => y).ToArray(), 0, arr.Length - 1, x) + " are greater than " + x);


            Console.Read();
        }
예제 #4
0
        public void BinarySearchGeneric_FindIndex_Test()
        {
            var arr = new double[] { 0, 1.3, 2, 3, 4, 5, 6.4, 7, 8, 9, 10.0 };

            // (val, expectedIndex)
            var testCases = new Tuple <double, int>[]
            {
                new Tuple <double, int>(1.3, 1),
                new Tuple <double, int>(0.0, 0),
                new Tuple <double, int>(10.0, 10),
                new Tuple <double, int>(6.4, 6)
            };

            var binarySearcher = new BinarySearch <double>();

            foreach (var testCase in testCases)
            {
                var result = binarySearcher.FindIndex(arr, testCase.Item1);

                Assert.That(result, Is.EqualTo(testCase.Item2));
            }
        }
예제 #5
0
        public void BinarySearch_FindIndex_Test()
        {
            var arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            // (val, expectedIndex)
            var testCases = new Tuple <int, int>[]
            {
                new Tuple <int, int>(1, 1),
                new Tuple <int, int>(0, 0),
                new Tuple <int, int>(10, 10),
                new Tuple <int, int>(6, 6)
            };

            var binarySearcher = new BinarySearch();

            foreach (var testCase in testCases)
            {
                var result = binarySearcher.FindIndex(arr, testCase.Item1);

                Assert.That(result, Is.EqualTo(testCase.Item2));
            }
        }
예제 #6
0
        static void Main(string[] args)
        {
            // bubble sort
            var array  = new int[] { 1, 4, 3, 7, 44, 34, 6, 11, 36, 43 };
            var sorted = BubbleSort.Sort(array);

            foreach (int item in sorted)
            {
                Console.Write(item.ToString());
            }

            // binary search

            var myArray = new int[] { 1, 2, 4, 6, 10, 14, 15, 19, 20, 34, 36, 38, 40, 42 };

            var value = BinarySearch.BSearch(10, myArray);

            // merge sort

            int[] mergeSorted = MergeSort.Sort(array);


            Console.ReadKey();
        }
예제 #7
0
 static void Main(string[] args)
 {
     Console.WriteLine(BinarySearch.SearchUsingBinary(new int[] { 1, 2, 3, 4, 5, 6, 10 }, 5));
     Console.WriteLine("Hello World!");
     Console.ReadKey();
 }
        static void Main(string[] args)
        {
            #region Binary Search
            //Let us create following BST
            //            50
            //         /      \
            //        30      70
            //      /  \     /  \
            //     20   40  60   80

            MyBinaryTree bTree = new MyBinaryTree();
            Node         node  = null;
            node = bTree.AddData(ref node, 50);
            node = bTree.AddData(ref node, 30);
            node = bTree.AddData(ref node, 70);
            node = bTree.AddData(ref node, 20);
            node = bTree.AddData(ref node, 40);
            node = bTree.AddData(ref node, 60);
            node = bTree.AddData(ref node, 80);

            int  height = bTree.CalulcateHeight(node);
            Node node1  = bTree.Search(node, 20);


            MyBinaryTree bTree2 = new MyBinaryTree();
            Node         node2  = null;
            bTree2.AddData(ref node2, 1);
            bTree2.AddData(ref node2, 2);
            bTree2.AddData(ref node2, 3);
            bTree2.AddData(ref node2, 4);
            bTree2.AddData(ref node2, 5);
            bTree2.AddData(ref node2, 6);
            bTree2.AddData(ref node2, 7);

            height = bTree.CalulcateHeight(node2);
            node1  = bTree.Search(node2, 5);

            #endregion

            #region BinarySearch on Array
            int[] values = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                values[i] = i;
            }

            //Array.BinarySearch(values, 665);
            int g = BinarySearch.MyBSearch(values, 0, 999, 665);
            #endregion


            #region Sorting Algorithm
            int[] arr = new int[10] {
                100, 50, 20, 40, 10, 60, 80, 70, 90, 30
            };

            //SortingAlgorithms.SelectionSort(ref arr);
            //SortingAlgorithms.InsertionSort(ref arr);
            //SortingAlgorithms.HeapSort(ref arr);
            SortingAlgorithms.MergeSort(ref arr, 0, arr.Length - 1);

            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            #endregion

            #region Searching Algorithm
            int value = SearchingAlgorithms.BinarySearch(arr, 20);

            #endregion
        }
예제 #9
0
        private static void BinarySearch()
        {
            var sort = new BinarySearch();

            sort.Run();
        }