示例#1
0
        internal static void Main(string[] args)
        {
            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("Linear search 131:");
            Console.WriteLine(collection.LinearSearch(131));
            Console.WriteLine();

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

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

            Console.WriteLine("Collection before shuffle:");
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

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

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
示例#2
0
        internal static void Main(string[] args)
        {
            var randomArr = GetRandomArr(15);

            randomArr[15 / 2] = 101;
            var collection = new SortableCollection <int>(randomArr);

            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>(randomArr);
            Console.WriteLine("Quicksorter result:");
            collection.Sort(new Quicksorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection <int>(randomArr);
            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("Binary search 301:");
            Console.WriteLine(collection.BinarySearch(301));
            Console.WriteLine();

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

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
示例#3
0
        internal static void Main(string[] args)
        {
            var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });

            Console.WriteLine("All items before search's and shuffleing:");
            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();
        }
示例#4
0
        public void BinarySearch()
        {
            var collection = new SortableCollection <int>(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            bool result = collection.BinarySearch(8);

            Assert.IsTrue(result);
        }
示例#5
0
        internal static void Main(string[] args)
        {
            var sw = new Stopwatch();

            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:");
            sw.Start();
            collection.Sort(new SelectionSorter <int>());
            Console.WriteLine("Time : {0}", sw.ElapsedTicks);
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

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

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

            Console.WriteLine("Linear search 101:");
            sw.Restart();
            Console.WriteLine(collection.LinearSearch(101));
            Console.WriteLine("Time : {0}", sw.ElapsedTicks);
            Console.WriteLine();

            Console.WriteLine("Binary search 101:");
            sw.Restart();
            Console.WriteLine(collection.BinarySearch(101));
            Console.WriteLine("Time : {0}", sw.ElapsedTicks);
            Console.WriteLine();

            Console.WriteLine("Shuffle:");
            sw.Restart();
            collection.Shuffle();
            Console.WriteLine("Time : {0}", sw.ElapsedTicks);
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("Shuffle again:");
            sw.Restart();
            collection.Shuffle();
            Console.WriteLine("Time : {0}", sw.ElapsedTicks);
            collection.PrintAllItemsOnConsole();
        }
示例#6
0
        internal static void Main(string[] args)
        {
            var collection = new SortableCollection <int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 });

            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, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 });
            Console.WriteLine("Quicksorter result:");
            var quickSorter = new Quicksorter <int>();

            collection.Sort(quickSorter);
            quickSorter.PrintResults();
            //collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection <int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 });
            Console.WriteLine("MergeSorter result:");
            var mergeSort = new MergeSorter <int>();

            collection.Sort(mergeSort);
            mergeSort.ShowResults();
            //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();


            collection = new SortableCollection <int>(new int[20]);
            for (int i = 1; i <= 20; i++)
            {
                collection.Items[i - 1] = i;
            }
            Console.WriteLine("All items before sorting:");
            collection.PrintAllItemsOnConsole();
            Console.WriteLine("Shuffle:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
示例#7
0
        public static void Main()
        {
            var data = new List <int> {
                5, 0, -10, 3, 11, 500, -23, 1000, 7
            };

            data.Sort();
            SortableCollection <int> s = new SortableCollection <int>(data);

            Console.WriteLine("Before ");
            Console.WriteLine(string.Join(" ", s.Items));

            s.Shuffle();
            Console.WriteLine("After shuffle ");
            Console.WriteLine(string.Join(" ", s.Items));
            Console.WriteLine(s.BinarySearch(-22));
        }
示例#8
0
        internal static void Main(string[] args)
        {
            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();

            // Fisher–Yates shuffle
            // Complexity O(n) https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
            Console.WriteLine("Shuffle:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

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