Esempio n. 1
0
        static void Main(string[] args)
        {
            long i1 = Environment.WorkingSet;
            int  fi = 4;
            long i3 = GC.GetTotalMemory(true);

            Int32[] m   = new Int32[100000000];
            int[]   arr = new int[4] {
                156, 215, 318, 50
            };
            int[] arr2 = new int[4] {
                156, 215, 318, 50
            };
            long   i4      = GC.GetTotalMemory(true);
            long   i2      = Environment.WorkingSet;
            long   raz     = i2 - i1;
            long   raz2    = i4 - i3;
            int    arrsize = 20;
            Random rnd     = new Random();

            LinkedList <int> list = new LinkedList <int>();

            for (int i = 0; i < arrsize; i++)
            {
                list.AddFirst(rnd.Next(0, arrsize));
            }
            //InsertSort_list(list);

            //Console.WriteLine("Origin array:" + list.ToString());
            Console.WriteLine("\n");
            Console.WriteLine("Origin array:");
            int[] rndarr = new int[arrsize];
            for (int i = 0; i < arrsize; i++)
            {
                rndarr[i] = rnd.Next(0, arrsize);
                Console.Write(rndarr[i] + " ");
            }
            int[]     rndarr1 = rndarr;
            int[]     rndarr2 = rndarr;
            int[]     rndarr3 = rndarr;
            Stopwatch sw1     = Stopwatch.StartNew();

            SelectionSort(rndarr1);
            sw1.Stop();
            Stopwatch sw2 = Stopwatch.StartNew();

            InsertSort(rndarr2);
            sw2.Stop();
            Stopwatch sw4        = Stopwatch.StartNew();
            var       sortedList = InsertionSort.Sort <int>(list);

            sw4.Stop();
            Stopwatch sw3 = Stopwatch.StartNew();

            BucketSort(rndarr3);
            sw3.Stop();
            //Console.WriteLine(i1);
            //Console.WriteLine(i2);
            //Console.WriteLine(raz);
            //Console.WriteLine(raz2);

            Console.WriteLine("\n");
            Console.WriteLine("Selection Sort:");
            for (int i = 0; i < arrsize; i++)
            {
                Console.Write(rndarr1[i] + " ");
            }
            Console.WriteLine("\nTime Elapsed: " + sw1.Elapsed);
            Console.WriteLine("\n");

            Console.WriteLine("Insert Sort:");
            for (int i = 0; i < arrsize; i++)
            {
                Console.Write(rndarr2[i] + " ");
            }
            Console.WriteLine("\nTime Elapsed: " + sw2.Elapsed);
            Console.WriteLine("\n");

            Console.WriteLine("Bucket Sort:");
            for (int i = 0; i < arrsize; i++)
            {
                Console.Write(rndarr3[i] + " ");
            }
            Console.WriteLine("\nTime Elapsed: " + sw3.Elapsed);
            Console.WriteLine("\n");

            Console.WriteLine("Origin list:");
            LinkedListNode <int> node;

            for (node = list.First; node != null; node = node.Next)
            {
                Console.Write(node.Value + " ");
            }
            Console.WriteLine("\n");

            Console.WriteLine("Insert Sort by linked list:");
            LinkedListNode <int> node2;

            for (node2 = sortedList.First; node2 != null; node2 = node2.Next)
            {
                Console.Write(node2.Value + " ");
            }
            Console.WriteLine("\nTime Elapsed: " + sw4.Elapsed);
            Console.WriteLine("\n");

            Console.WriteLine("Factorial from " + fi + " = " + Factor(fi));
            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var tree1 = new Tree(1);
            var tree2 = new Tree(2);
            var tree3 = new Tree(3);
            var tree4 = new Tree(4);

            tree1.AddConnection(tree2);
            tree1.AddConnection(tree3);
            tree2.AddConnection(tree4);
            //tree4.AddConnection(tree1);

            BreadthFirstSearch.Traverse(tree1);
            DepthFirstSearch.Traverse(tree1);

            var binaryTreecoc = new BinaryTreeCocoon();

            binaryTreecoc.Insert(100);
            binaryTreecoc.Insert(10);
            binaryTreecoc.Insert(200);
            binaryTreecoc.Insert(400);
            binaryTreecoc.Insert(50);
            binaryTreecoc.Insert(5);
            binaryTreecoc.Insert(8);
            binaryTreecoc.Insert(1);
            binaryTreecoc.Traverse();
            binaryTreecoc.ReversTraverse();
            binaryTreecoc.LeftViewTraverse();
            binaryTreecoc.RightViewTraverse();


            var linkedlist = new CustomLinkedList(1);

            linkedlist.Next           = new CustomLinkedList(2);
            linkedlist.Next.Next      = new CustomLinkedList(3);
            linkedlist.Next.Next.Next = linkedlist.Next;

            var ops = new LinkedListOperations();

            Console.WriteLine("\r\nCheckCycle");
            Console.WriteLine(ops.CheckIfCyclic(linkedlist));
            ops.BreakCheckIfCyclic(linkedlist);

            var mimStack = new MinimumStackOrderOfOne();

            Console.WriteLine("\r\nMinimumStack");
            mimStack.Push(10);
            mimStack.Push(100);
            Console.WriteLine(mimStack.GetMinimum());
            mimStack.Push(5);
            mimStack.Push(20);
            mimStack.Push(30);
            Console.WriteLine(mimStack.GetMinimum());
            mimStack.Pop();
            mimStack.Pop();
            mimStack.Pop();
            Console.WriteLine(mimStack.GetMinimum());
            mimStack.Push(10000);
            mimStack.Push(50);

            var data = new SortedArrayToBST().Run();

            var sortedss = SelectionSort.Sort(new int[] { 100, 200, 20, 4, 10, 35, });

            Console.WriteLine($"\r\nSelectionSort : {JsonConvert.SerializeObject(sortedss)} \r\n");
            var sortedis = InsertionSort.Sort(new int[] { 100, 200, 20, 4, 10, 35, });

            Console.WriteLine($"\r\nInsertionSort : {JsonConvert.SerializeObject(sortedis)} \r\n");

            var arr = new int[] { 100, 200, 20, 4, 10, 35, };

            Console.WriteLine($"\r\nHeapSortSort Initials : {JsonConvert.SerializeObject(arr)} \r\n");
            var sortedhs = HeapSort.Sort(arr);

            Console.WriteLine($"\r\nHeapSortSort : {JsonConvert.SerializeObject(sortedhs)} \r\n");
            var ar2       = new int[] { 100, 200, 20, 4, 10, 35, };
            var sortedhs2 = HeapSort.Sort2(ar2);

            Console.WriteLine($"\r\nHeapSortSortGeks : {JsonConvert.SerializeObject(sortedhs2)} \r\n");


            var ar3      = new int[] { 100, 200, 20, 4, 10, 35, };
            var sortedqs = QuickSort.Sort(ar2);

            Console.WriteLine($"\r\n QuickSort : {JsonConvert.SerializeObject(sortedqs)} \r\n");

            Console.ReadKey();
        }