예제 #1
0
        public void Run()
        {
            for (var k = 1; k < 10; k++)
            {
                _c = 0;
                var stack = new Stack <int>();

                for (var i = 0; i < 10 * k; i++)
                {
                    var randomNum = AssortedMethods.RandomIntInRange(0, 1000);
                    stack.Push(randomNum);
                }

                stack = Mergesort(stack);
                var last = int.MaxValue;

                while (stack.Count != 0)
                {
                    var curr = stack.Pop();

                    if (curr > last)
                    {
                        Console.WriteLine("Error: " + last + " " + curr);
                    }
                    last = curr;
                }

                Console.WriteLine(_c);
            }
        }
예제 #2
0
파일: Q03_6.cs 프로젝트: zs9912/ctci
 public void Run()
 {
     for (int k = 1; k < 10; k++)
     {
         _c = 0;
         Stack <int> s = new Stack <int>();
         for (int i = 0; i < 10 * k; i++)
         {
             int r = AssortedMethods.RandomIntInRange(0, 1000);
             s.Push(r);
         }
         s = Mergesort(s);
         int last = int.MaxValue;
         while (s.Count != 0)
         {
             int curr = s.Pop();
             if (curr > last)
             {
                 Console.WriteLine("Error: " + last + " " + curr);
             }
             last = curr;
         }
         Console.WriteLine(_c);
     }
 }
 public static int[][] Matrix_Get2DArrayRandom(int rowsCount, int columnCounts, int minNumber, int maxNumber)
 {
     int[][] matrix = new int[rowsCount][];
     for (int i = 0; i < rowsCount; i++)
     {
         matrix[i] = new int[columnCounts];
         for (int j = 0; j < columnCounts; j++)
         {
             matrix[i][j] = AssortedMethods.RandomIntInRange(minNumber, maxNumber);
         }
     }
     return(matrix);
 }
        public static MyLinkedListNode GetLinkedListSingly_Random(int N, int min, int max)
        {
            MyLinkedListNode root = new MyLinkedListNodeSingly(AssortedMethods.RandomIntInRange(min, max), null);
            MyLinkedListNode prev = root;

            for (int i = 1; i < N; i++)
            {
                int data = AssortedMethods.RandomIntInRange(min, max);
                MyLinkedListNode next = new MyLinkedListNodeSingly(data, null);
                prev.SetNext(next);
                prev = next;
            }
            return(root);
        }
예제 #5
0
        public void Run()
        {
            var myQueue = new MyQueue <int>();

            // Let's test our code against a "real" queue
            var testQueue = new Queue <int>();

            for (var i = 0; i < 100; i++)
            {
                var choice = AssortedMethods.RandomIntInRange(0, 10);

                if (choice <= 5)
                {
                    // enqueue
                    var element = AssortedMethods.RandomIntInRange(1, 10);
                    testQueue.Enqueue(element);
                    myQueue.Enqueue(element);
                    Console.WriteLine("Enqueued " + element);
                }
                else if (testQueue.Count > 0)
                {
                    var top1 = testQueue.Dequeue();
                    var top2 = myQueue.Dequeue();

                    if (top1 != top2)
                    { // Check for error
                        Console.WriteLine("******* FAILURE - DIFFERENT TOPS: " + top1 + ", " + top2);
                    }
                    Console.WriteLine("Dequeued " + top1);
                }

                if (testQueue.Count == myQueue.Size())
                {
                    if (testQueue.Count > 0 && testQueue.Peek() != myQueue.Peek())
                    {
                        Console.WriteLine("******* FAILURE - DIFFERENT TOPS: " + testQueue.Peek() + ", " + myQueue.Peek() + " ******");
                    }
                }
                else
                {
                    Console.WriteLine("******* FAILURE - DIFFERENT SIZES ******");
                }
            }
        }
예제 #6
0
파일: Q03_2.cs 프로젝트: zs9912/ctci
        public void Run()
        {
            StackWithMin  stack  = new StackWithMin();
            StackWithMin2 stack2 = new StackWithMin2();

            for (int i = 1; i <= 10; i++)
            {
                int value = AssortedMethods.RandomIntInRange(0, 100);
                stack.Push2(value);
                stack2.Push2(value);
                Console.Write(value + ", ");
            }
            Console.WriteLine('\n');
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("Popped " + stack.Pop().Value + ", " + stack2.Pop2());
                Console.WriteLine("New min is " + stack.Min() + ", " + stack2.Min());
            }
        }
예제 #7
0
        public void Run()
        {
		    // Create balanced tree
		    int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
		    var root = TreeNode.CreateMinimalBst(array);
		    Console.WriteLine("Root? " + root.Data);
		    Console.WriteLine("Is balanced? " + IsBalanced(root));
            Console.WriteLine("Improved Is balanced? " + IsBalancedImproved(root));
		
		    // Could be balanced, actually, but it's very unlikely...
		    var unbalanced = new TreeNode(10);
		    for (var i = 0; i < 10; i++) 
            {
			    unbalanced.InsertInOrder(AssortedMethods.RandomIntInRange(0, 100));
		    }

		    Console.WriteLine("Root? " + unbalanced.Data);
            Console.WriteLine("Is balanced? " + IsBalanced(unbalanced));
            Console.WriteLine("Improved Is balanced? " + IsBalancedImproved(unbalanced));
        }
예제 #8
0
        public static BinarySearchTreeNode Create_Random(int N, int min, int max)
        {
            int[] dataArray           = new int[N];
            int   d                   = AssortedMethods.RandomIntInRange(min, max);
            BinarySearchTreeNode root = new BinarySearchTreeNode(d);

            dataArray[0] = d;
            for (int i = 1; i < N; i++)
            {
                dataArray[i] = AssortedMethods.RandomIntInRange(min, max);
            }
            dataArray.Print();
            for (int i = 1; i < N; i++)
            {
                root.InsertInOrder(dataArray[i]);
                root.Print();
            }
            return(root);

            //return CreateMinimalBinarySearchTree(dataArray);
        }