示例#1
0
 private static void TestSearchAlgorithms(SortableCollection <int> collection, int searches)
 {
     collection.Sort(new QuickSorter <int>());
     for (int i = 0; i < searches; i++)
     {
         var searchedNumber = GetRandomNumber();
         Console.WriteLine("Linear search: {0} Found: {1}", searchedNumber, collection.LinearSearch(searchedNumber));
         Console.WriteLine("Binary search: {0} Found: {1}", searchedNumber, collection.BinarySearch(searchedNumber));
     }
 }
        internal static void Main()
        {
            var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });

            Console.WriteLine("All items before sorting:");
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("SelectionSorter result:");
            collection.Sort(new SelectionSorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("Quicksorter result:");
            collection.Sort(new Quicksorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("MergeSorter result:");
            collection.Sort(new MergeSorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("Linear search 101:");
            Console.WriteLine(collection.LinearSearch(101));
            Console.WriteLine();

            Console.WriteLine("Binary search 101:");
            Console.WriteLine(collection.BinarySearch(101));
            Console.WriteLine();

            Console.WriteLine("Shuffle:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }