Beispiel #1
0
        static void Main(string[] args)
        {
            // TODO Open file for reading
            string[] lines = System.IO.File.ReadAllLines("./../../input/words.txt");
            //string[] lines = System.IO.File.ReadAllLines("./../../input/phil.txt");

            // Create or erase output file
            File.Create("./../../input/output.txt").Close();
            StreamWriter file = new System.IO.StreamWriter("./../../input/output.txt", true);

            // TODO Initialize hash table
            MyHashTable table = new MyHashTable(128);

            // While less than 40% full, add entries to table
            int CurrentLine = 0;

            while (table.CurrentCapacity < (table.Length * .4d))
            {
                table.Add(lines[CurrentLine++]);
            }
            //while (table.CurrentCapacity < (table.Length * .99d)) { table.Add(lines[CurrentLine++]); }

            // TODO Get min, max, and avg #probes for the first 30 words
            CurrentLine = 0;
            int ProbeSum = 0;
            int ProbeMin = table.Length;
            int ProbeMax = -1;

            while (CurrentLine < table.CurrentCapacity)
            {
                MyHashNode retrieved = table.Get(lines[CurrentLine]);
                ProbeSum += retrieved.ProbeCount;
                ProbeMin  = retrieved.ProbeCount < ProbeMin ? retrieved.ProbeCount : ProbeMin;
                ProbeMax  = retrieved.ProbeCount > ProbeMax ? retrieved.ProbeCount : ProbeMax;
                CurrentLine++;
            }
            // TODO Get min, max, and avg #probes for the last 30 words

            // TODO Print table
            Console.WriteLine(table.ToString());
            file.Write(table.ToString());

            //
            file.WriteLine($"Minimum number of probes: {ProbeMin}");
            file.WriteLine($"Maximum number of probes: {ProbeMax}");
            file.WriteLine($"average number of probes: {(double)ProbeSum / table.CurrentCapacity}");
            file.Close();
            Console.WriteLine($"Minimum number of probes: {ProbeMin}");
            Console.WriteLine($"Maximum number of probes: {ProbeMax}");
            Console.WriteLine($"average number of probes: {(double)ProbeSum / table.CurrentCapacity}");
            double a = (double)table.CurrentCapacity / (double)table.Length;
            double E = (1 - a / 2) / (1 - a);

            Console.WriteLine($"Load factor (alpha): {a}");
            Console.WriteLine($"Expected number of probes: {E}");

            // End program
            Console.ReadKey();
            Environment.Exit(0);
        }
        static void Main(string[] args)
        {
            Action <Array> PrintArray = (a) => {
                string pretty_string = "[";
                foreach (var c in a)
                {
                    pretty_string += c.ToString() + ", ";
                }
                pretty_string += "]";
                System.Console.WriteLine(pretty_string);
            };

            //Test search algorithms
            int[] array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
            System.Console.WriteLine(array1.SequentialSearch(10));
            System.Console.WriteLine(array1.BinarySearch(20));

            //Test sort algorithms
            //BubleSort
            int[] array2 = new int[] { 45, 3, 5, 77, 4, 2, 9, 1, 7, 53, 75, 14, 56, 45, 453, 63, 83, 12, 11, 45, 90, 78 };
            array2.BubleSort();
            PrintArray(array2);
            //Insertion sort
            int[] array3 = new int[] { 6, 87, 432, 23, 85, 15, 3, 78, 1, 56, 90, 3, 4, 763, 73, 83, 23, 52 };
            array3.InsertionSort();
            PrintArray(array3);
            //Merge sort
            int[] array4 = new int[] { 56, 89, 2, 45, 90, 983, 23, 12, 323, 15, 48, 73, 98, 101, 76, 54, 63, 74, 7, 17, 4, 3, 5, 83 };
            array4.MergeSort(0, array4.Length - 1);
            PrintArray(array4);

            //Test LinkedList (Stack)
            MyLinkedList <int> list1 = new MyLinkedList <int>();

            list1.Push(1); //Push one on top of the stack [1,]
            list1.Push(2); //Push two on top [2,1]
            list1.Push(3); //Push three on top [3,2,1,]
            System.Console.WriteLine(list1.ToString());

            //Test DoublyLinkedList (Que)
            MyDoublyLinkedList <int> list2 = new MyDoublyLinkedList <int>();

            list2.Enqueue(1); //Puts a one at the back of the que [1, ]
            list2.Enqueue(2); //Puts a two at the back of the que [1,2, ]
            list2.Enqueue(3); //Puts a three at the back of the que [1,2,3, ]
            System.Console.WriteLine(list2.ToString());

            //Test HashTable
            //Let the numbers by phonenumbers, SSN's, studentnumbers etc.
            //Search for the key name to find the corresponding number
            MyHashTable <int> hashTable1 = new MyHashTable <int>();

            hashTable1.Insert("Steven", 13232);
            hashTable1.Insert("Bob", 56732);
            hashTable1.Insert("John", 83832);
            hashTable1.Insert("Frank", 37723);
            hashTable1.Insert("Allice", 43433);
            hashTable1.Insert("Donald", 32342);
            System.Console.WriteLine(hashTable1.ToString());
            //Searching doesn't work
            //System.Console.WriteLine("Found value: " + hashTable1.SearchValueByKey("Steven"));

            //Test BinaryTree (BinarySearchTree)
            MyTree tree1 = new MyTree();

            tree1.Insert(1);
            tree1.Insert(3);
            tree1.Insert(5);
            tree1.Insert(7);
            tree1.Insert(9);
            tree1.Insert(11);
            tree1.Insert(13);
            tree1.Insert(2);
            tree1.Insert(4);
            tree1.Insert(6);
            tree1.Insert(8);
            tree1.Insert(10);
            tree1.Insert(12);
            tree1.Insert(14);
            tree1.PretyPrint();
        }