示例#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
文件: Program.cs 项目: mebjas/ADA
        static void Main(string[] args)
        {
            int[] data = new int[] { 5, 2, 1, 4, 3, 5, 0, 9, 5, 4, 3, 2, 1, 0 };

            Console.WriteLine("Heap Sort");
            int[] sorted1 = new HeapSort(data, data.Length).Sort();
            HeapSort.PrintArray(sorted1, sorted1.Length);

            Console.WriteLine("Quick Sort");
            int[] sorted2 = new QuickSort(data, data.Length).Sort();
            QuickSort.PrintArray(sorted1, sorted1.Length);

            Console.WriteLine("Merge Sort");
            int[] sorted3 = new MergeSort(data, data.Length).Sort();
            MergeSort.PrintArray(sorted1, sorted1.Length);

            Console.WriteLine("Graph - BFS");
            Graph g = new Graph(4);

            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(1, 2);
            g.AddEdge(2, 0);
            g.AddEdge(2, 3);
            g.AddEdge(3, 3);

            g.BFS(2, (i) => Console.Write("{0} ", i));
            Console.WriteLine(string.Empty);

            Console.WriteLine("Graph - DFS");
            g.DFS(2, (i) => Console.Write("{0} ", i));
            Console.WriteLine(string.Empty);

            Graph g1 = new Graph(9);
            List <List <int> > g2 = new List <List <int> >()
            {
                new List <int>()
                {
                    0, 4, 0, 0, 0, 0, 0, 8, 0
                },
                new List <int>()
                {
                    4, 0, 8, 0, 0, 0, 0, 11, 0
                },
                new List <int>()
                {
                    0, 8, 0, 7, 0, 4, 0, 0, 2
                },
                new List <int>()
                {
                    0, 0, 7, 0, 9, 14, 0, 0, 0
                },
                new List <int>()
                {
                    0, 0, 0, 9, 0, 10, 0, 0, 0
                },
                new List <int>()
                {
                    0, 0, 4, 14, 10, 0, 2, 0, 0
                },
                new List <int>()
                {
                    0, 0, 0, 0, 0, 2, 0, 1, 6
                },
                new List <int>()
                {
                    8, 11, 0, 0, 0, 0, 1, 0, 7
                },
                new List <int>()
                {
                    0, 0, 2, 0, 0, 0, 6, 7, 0
                }
            };

            for (int i = 0; i < g2.Count; i++)
            {
                for (int j = 0; j < g2[i].Count; j++)
                {
                    if (g2[i][j] > 0)
                    {
                        g1.AddEdge(i, j, g2[i][j]);
                    }
                }
            }

            g1.PrintDistance(g1.Dijakstra(0), 0);
            Console.WriteLine(string.Empty);

            Console.WriteLine("Binary Search");
            int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            foreach (int i in new int[] { -5, 1, 5, 8, 0, 10, 11 })
            {
                Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearch(arr, i));
            }

            Console.WriteLine("Binary Search Next greatest - 1");
            int[] arr2 = new int[] { 100, 50, 40, 20, 10 };
            foreach (int i in new int[] { 5, 25, 50, 120 })
            {
                Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearchNextGreatest(arr2, i));
            }

            Console.WriteLine("Binary Search Next greatest - 2");
            int[] arr3 = new int[] { 100, 90, 80, 75, 60 };
            foreach (int i in new int[] { 50, 65, 77, 90, 102 })
            {
                Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearchNextGreatest(arr3, i));
            }

            Console.ReadKey();
        }