コード例 #1
0
        public static void Main()
        { // driver chunk
            string input = UI.PromptLine(
                "Please enter some comma/space separated integers: ");

            int[] data = ExtractFromString.IntsFromString(input);
            Sorting.IntArrayShellSortBetter(data);
            string prompt =
                "Please enter a number to find (empty line to end): ";

            input = UI.PromptLine(prompt);
            while (input.Length != 0)
            {
                int searchItem = int.Parse(input);
                int foundPos   = BinarySearching.IntArrayBinarySearchPrinted(
                    data, searchItem);
                if (foundPos < 0)
                {
                    Console.WriteLine("Item {0} not found", searchItem);
                }
                else
                {
                    Console.WriteLine("Item {0} found at position {1}",
                                      searchItem, foundPos);
                }
                input = UI.PromptLine(prompt);
            }
        }                               // end drive chunk
コード例 #2
0
        // chunk-experiment1-end

        public static long ExperimentIntArrayBinarySearch(int n, int rep, int seed)
        {
            Stopwatch watch = new Stopwatch();

            int[] data = new int[n];
            Sorting.IntArrayGenerate(data, seed);
            // Use our existing work on sorting to generate sorted array for testing
            Sorting.IntArrayQuickSort(data);
            watch.Reset();
            watch.Start();
            int m = Math.Max(1, n / rep);

            // perform the rep lookups
            for (int k = 0, i = 0; k < rep; k++, i = (i + m) % n)
            {
                BinarySearching.IntArrayBinarySearch(data, data [i]);
            }
            watch.Stop();
            return(watch.ElapsedMilliseconds);
        }