Exemple #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("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();
        }
Exemple #2
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();
        }
Exemple #3
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);
        }
Exemple #4
0
        public void LinearSearchLast()
        {
            var collection = new SortableCollection <int>(new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            bool result = collection.LinearSearch(0);

            Assert.IsTrue(result);
        }
Exemple #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();
        }
Exemple #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();
        }
Exemple #7
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();

            collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });
            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("BubbleSorter result:");
            collection.Sort(new BubbleSorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("InsertionSorter result:");
            collection.Sort(new InsertionSorter <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.LinearSearch(101));
            Console.WriteLine();

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

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
Exemple #8
0
        public void MergeSort()
        {
            var collection = new SortableCollection <int>(new[] { 5, 1, 2, 4, 0, 6, 9, 8, 7 });

            collection.Sort(new MergeSorter <int>());

            for (int i = 0; i < collection.Items.Count - 1; i++)
            {
                Assert.IsTrue(collection.Items[i] < collection.Items[i + 1]);
            }
        }
Exemple #9
0
        private static void CalculateProceedingTime(int numberOfElementsInCollection, ISorter <int> sorter)
        {
            var numbers    = GenerateCollection(numberOfElementsInCollection);
            var collection = new SortableCollection <int>(numbers);

            collection.Shuffle();
            Stopwatch st = new Stopwatch();

            st.Start();
            collection.Sort(sorter);
            st.Stop();

            Console.WriteLine($"Ellapsed time for sorting {numberOfElementsInCollection} integers: {st.Elapsed}");
        }
Exemple #10
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();
        }
Exemple #11
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();

            ///
            /// Uncomment the sort you want to see and leave
            /// others commented
            ///

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

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

            //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();
        }
Exemple #12
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));
        }
        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();

            // Could not keep the reference to collection in MergeSort => Quicksorting to proceed!
            collection.Sort(new Quicksorter <int>());

            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();
        }
Exemple #14
0
        public void Shuffle()
        {
            var collection = new SortableCollection <int>(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            collection.Shuffle();
            bool shuffled = false;

            for (int i = 0; i < collection.Items.Count - 1; i++)
            {
                if (collection.Items[i] > collection.Items[i + 1])
                {
                    shuffled = true;
                    break;
                }
            }

            Assert.IsTrue(shuffled);
        }
Exemple #15
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();
        }
Exemple #16
0
        internal static void Main(string[] args)
        {
            var collection = new SortableCollection <int>(new[] { 77, 332, 651, 98, 1, 5454, 34, 1, 656, 1, 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[] { 77, 332, 651, 98, 1, 5454, 34, 1, 656, 1, 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("Quicksorter result:");
            collection.Sort(new Quicksorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection <int>(new[] { 77, 332, 651, 98, 1, 5454, 34, 1, 656, 1, 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("MergeSorter result:");
            collection.Sort(new MergeSorter <int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

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

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

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

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
Exemple #17
0
        public static void Main()
        {
            SortableCollection <int> collection = new SortableCollection <int>(GenerateListOfInts());

            collection.Shuffle(collection.Items);
        }