示例#1
0
        static void Main(string[] args)
        {
            //Trees and Graph Algorithms
            BinarySearchTree.Test(10);
            GraphSearch.Test();

            //Test data
            int[] data     = Helper.Generate(10, 1000);
            int[] heapData = new int[] { 12, 11, 13, 5, 6, 7 };

            //Sorting Algorithms
            int[] bubbleSort    = Sort.BubbleSort(data);
            int[] selectionSort = Sort.SelectionSort(data);
            int[] insertionSort = Sort.InsertionSort(data);
            int[] mergeSort     = Sort.MergeSort(data);
            int[] quickSort     = Sort.QuickSort(data, 0, data.Length / 2);
            int[] heapSort      = Sort.HeapSort(heapData);

            //Searching Algorithms
            int value                 = mergeSort[mergeSort.Length / 2];
            int binarySearch          = Search.BinarySearch(mergeSort, value);
            int binarySearchRecursive = Search.BinarySearchRecursive(mergeSort, value, 0, mergeSort.Length - 1);

            int[,] dGraph =
            {
                { 0,  6, 0,  0,  0,  0, 0,  9, 0 },
                { 6,  0, 9,  0,  0,  0, 0, 11, 0 },
                { 0,  9, 0,  5,  0,  6, 0,  0, 2 },
                { 0,  0, 5,  0,  9, 16, 0,  0, 0 },
                { 0,  0, 0,  9,  0, 10, 0,  0, 0 },
                { 0,  0, 6,  0, 10,  0, 2,  0, 0 },
                { 0,  0, 0, 16,  0,  2, 0,  1, 6 },
                { 9, 11, 0,  0,  0,  0, 1,  0, 5 },
                { 0,  0, 2,  0,  0,  0, 6,  5, 0 }
            };

            GraphTraversal.Dijkstra(dGraph, 0, 9);


            int[, ] pGraph = new int[, ] {
                { 0, 2, 0, 6, 0 },
                { 2, 0, 3, 8, 5 },
                { 0, 3, 0, 0, 7 },
                { 6, 8, 0, 0, 9 },
                { 0, 5, 7, 9, 0 }
            };

            GraphTraversal.PrimMST(pGraph, 5);


            //Word Sorter
            BinaryTree <string> tree = new BinaryTree <string>();
            string input             = string.Empty;

            while (!input.Equals("quit", StringComparison.CurrentCultureIgnoreCase))
            {
                //read the line from the user
                Console.Write("> ");
                input = Console.ReadLine();
                //split the line into words (on space)
                string[] words = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                //add each word to the tree

                foreach (string word in words)
                {
                    tree.Add(word);
                }

                //print the number of words
                Console.WriteLine("{0} words", tree.Count);

                //add print each word using the default (in-order enumerator)
                foreach (string word in tree)
                {
                    Console.Write("{0} ", word);
                }

                Console.WriteLine();

                tree.Clear();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            //Stopwatch stopwatch = new Stopwatch();
            //string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            //int count = Recursion.CountOfTxtFilesInDir(path);
            //Console.WriteLine($@"Txt file count in dir {path} = {count}");

            //Recursion.PrintTxtFilesInDir(@"C:\Users\yornstein\Desktop");

            //stopwatch.Start();
            //Console.WriteLine($"Fib(5) = {Memoization.FibRecursion(5)}");
            //Console.WriteLine($"Fib(9) = {Memoization.FibRecursion(9)}");
            //Console.WriteLine($"Fib(15) = {Memoization.FibRecursion(15)}");
            //Console.WriteLine($"Fib(35) = {Memoization.FibRecursion(35)}");
            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.Elapsed);


            //stopwatch.Reset();
            //stopwatch.Start();
            //Console.WriteLine($"Fib(5) = {Memoization.FibMemoized(5)}");
            //Console.WriteLine($"Fib(9) = {Memoization.FibMemoized(9)}");
            //Console.WriteLine($"Fib(15) = {Memoization.FibMemoized(15)}");
            //Console.WriteLine($"Fib(35) = {Memoization.FibMemoized(35)}");
            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.Elapsed);

            //int[,] grid = new int[,]
            //{
            //    {0, 0, 0, 0, 0, 0, 0, 0},
            //    {0, 0, 1, 0, 0, 0, 1, 0},
            //    {0, 0, 0, 0, 1, 0, 0, 0},
            //    {1, 0, 1, 0, 0, 1, 0, 0},
            //    {0, 0, 1, 0, 0, 0, 0, 0},
            //    {0, 0, 0, 1, 1, 0, 1, 0},
            //    {0, 1, 0, 0, 0, 1, 0, 0},
            //    {0, 0, 0, 0, 0, 0, 0, 0},
            //};

            //Console.WriteLine($"PathsCount Recursion {DynamicProgramming.PathsCountRecursion(grid, 0, 0)}");
            //Console.WriteLine($"PathsCount Memoized {DynamicProgramming.PathsCountMemoized(grid, 0, 0)}");
            //Console.WriteLine($"PathsCount DynamicProg {DynamicProgramming.PathsCountDynamicProg(grid, 0, 0)}");

            //Console.ReadKey();

            //int[] nums = new int[] { 2, 3, 4, 5, 6, 7, 8, 10, 11, 14, 16 };
            //int[] nums2 = new int[] { 1, 3, 4, 5, 7, 8, 10, 12, 15, 18, 21, 22, 25 };

            //Console.WriteLine(Search.BinarySearchRecursion(nums, 9));
            //Console.WriteLine(Search.BinarySearchRecursion(nums2, 5));
            //Console.WriteLine(Search.BinarySearchRecursion(nums, 9, 0, nums.Length - 1));
            //Console.WriteLine(Search.BinarySearchRecursion(nums2, 5, 0, nums2.Length - 1));
            //Console.WriteLine(Search.BinarySearchLoop(nums, 9));
            //Console.WriteLine(Search.BinarySearchLoop(nums2, 5));

            //Random random = new Random();
            //int[] numbers = new int[1000];
            //for (int i = 0; i < 999; i++)
            //{
            //    int rand = random.Next(1, 10000);
            //    if (rand != 455)
            //        numbers[i] = rand;
            //}

            //int value = numbers[501];

            //Array.Sort(numbers);

            //Console.WriteLine(Search.BinarySearchRecursion(numbers, value));
            //Console.WriteLine(Search.BinarySearchRecursion(numbers, 455));
            //Console.WriteLine(Search.BinarySearchRecursion(numbers, value, 0, numbers.Length - 1));
            //Console.WriteLine(Search.BinarySearchRecursion(numbers, 455, 0, numbers.Length - 1));
            //Console.WriteLine(Search.BinarySearchLoop(numbers, value));
            //Console.WriteLine(Search.BinarySearchLoop(numbers, 455));

            //Console.ReadKey();

            int[] arr = new int[] { 4, 6, 3, 2, 7, 1, 5, 9, 8 };
            arr.ToList().ForEach(x => Console.Write(x));

            Console.WriteLine();

            Sort.BubbleSort(arr);
            arr.ToList().ForEach(x => Console.Write(x));

            Console.WriteLine("\n---------");

            arr = new int[] { 4, 6, 3, 2, 7, 1, 5, 9, 8, 10 };
            arr.ToList().ForEach(x => Console.Write(x));

            Console.WriteLine();

            Sort.MergeSort(arr);
            arr.ToList().ForEach(x => Console.Write(x));

            Console.WriteLine("\n---------");

            arr = new int[] { 4, 6, 3, 2, 7, 1, 5, 9, 8, 10 };
            arr.ToList().ForEach(x => Console.Write(x));

            Console.WriteLine();

            Sort.QuickSort(arr);
            arr.ToList().ForEach(x => Console.Write(x));
        }