示例#1
0
        private static int CountDistinctPairDiffByK(List <int> array, int k)
        {
            int         count = 0;
            ISort <int> sort  = new MergeSort <int>(array.ToArray());

            sort.Sort();
            array = sort.Array.ToList();
            ISearch <int> searcher = new BinarySearch <int>();

            for (int i = 0; i < array.Count; i++)
            {
                if (i - 1 >= 0 && array[i] == array[i - 1])
                {
                    continue;
                }

                int idx = searcher.Search(array[i] + k, array.ToArray());
                if (idx >= 0)
                {
                    count++;
                }
            }

            return(count);
        }
示例#2
0
        private static void BinarySearchTest(int[] arr, int valueToFind)
        {
            bool found = false;

            Console.WriteLine("Binary Search Test");

            ISearchable binarySearch = new BinarySearch();

            binarySearch.StartTimer();

            found = binarySearch.Search(arr, valueToFind);

            binarySearch.StopTimer();

            //Console.WriteLine($"Found value {valueToFind} : {found} ");

            if (_printArray)
            {
                binarySearch.PrintArray(arr);
            }

            binarySearch.PrintElapsedTime();

            binarySearch.ResetTimer();
        }
示例#3
0
        public static void Main(string[] args)
        {
            int x = 50;

            int[]        array        = new int[] { 10, 20, 30, 40, 50, 60, 70 };
            BinarySearch binarySearch = new BinarySearch();
            var          ret          = binarySearch.Search(array, x);
        }
示例#4
0
        private static void ExecuteBinarySearch()
        {
            var array = new int[] { 1, 3, 4, 8, 14, 21 };
            var item  = 56;

            Console.WriteLine($"Array - {string.Join(",", array)}");
            Console.WriteLine($"Binary Search - {item}");
            var algo  = new BinarySearch();
            var index = algo.Search(array, item);

            Console.WriteLine($"Item {item} present at index- {index}");
        }
示例#5
0
 private static void TestBinarySearch()
 {
     Console.WriteLine(2 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5 }, 3));
     Console.WriteLine(1 == BinarySearch.Search(new int[] { 1, 3, 4, 5, 6, 7 }, 3));
     Console.WriteLine(2 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5, 6 }, 3));
     Console.WriteLine(1 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5 }, 2));
     Console.WriteLine(1 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5, 6 }, 2));
     Console.WriteLine(4 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5 }, 5));
     Console.WriteLine(4 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5, 6 }, 5));
     Console.WriteLine(0 == BinarySearch.Search(new int[] { 1, 2, 3, 4, 5, 6 }, 1));
     Console.WriteLine(-1 == BinarySearch.Search(new int[] { }, 3));
     Console.WriteLine(-1 == BinarySearch.Search(new int[] { 1 }, 3));
     Console.WriteLine(0 == BinarySearch.Search(new int[] { 1 }, 1));
 }