Example #1
0
        public void TestReverse()
        {
            int[] inputArr    = { 1, 2, 3, 4, 5 };
            int[] expectedArr = { 5, 4, 3, 2, 1 };

            ArrayUtils.Reverse(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void ZerosRearrange()
        {
            int[] inputArr    = { 1, 2, 0, 4, 3, 0, 5, 0 };
            int[] expectedArr = { 1, 2, 4, 3, 5, 0, 0, 0 };

            rearrangement.ZerosRearrange(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void PosNegInOrderRearrange()
        {
            int[] inputArr    = { -5, -2, 5, 2, 4, 7, 1, 8, 0, -8 };
            int[] expectedArr = { -5, 5, -2, 2, -8, 4, 7, 1, 8, 0 };

            rearrangement.PosNegInOrderRearrange(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void Rearrange()
        {
            int[] inputArr    = { -1, -1, 6, 1, 9, 3, 2, -1, 4, -1 };
            int[] expectedArr = { -1, 1, 2, 3, 4, -1, 6, -1, -1, 9 };

            rearrangement.Rearrange(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void RotationShouldWorkIfRotationCountIsDivisibleByArraySize()
        {
            var d = 14;

            int[] inputArr    = { 1, 2, 3, 4, 5, 6, 7 };
            int[] expectedArr = { 1, 2, 3, 4, 5, 6, 7 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void RotationShouldWorkIfRotationCountIsGreaterThanArraySize()
        {
            var d = 24;

            int[] inputArr    = { 1, 2, 3, 4, 5, 6, 7 };
            int[] expectedArr = { 4, 5, 6, 7, 1, 2, 3 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void RotationShouldWorkForArrayOfSizeTwo()
        {
            var d = 1;

            int[] inputArr    = { 1, 2 };
            int[] expectedArr = { 2, 1 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void RotationShouldWork()
        {
            var d = 2;

            int[] inputArr    = { 1, 2, 3, 4, 5, 6, 7 };
            int[] expectedArr = { 3, 4, 5, 6, 7, 1, 2 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void ReorderArrays()
        {
            int[] inputArr   = { 10, 11, 12 };
            int[] inputIndex = { 1, 0, 2 };

            int[] expectedArr   = { 11, 10, 12 };
            int[] expectedIndex = { 0, 1, 2 };

            rearrangement.ReorderArrays(inputArr, inputIndex);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
            Assert.True(ArrayTestUtils.ArraysEqual(inputIndex, expectedIndex));
        }
        public void MinHeapify()
        {
            int[] inputArr    = { 7, 10, 4, 3, 20, 15 };
            int[] expectedArr = { 3, 4, 7, 10, 20, 15 };

            minHeap = new MinHeap(inputArr.Length);
            for (int i = 0; i < inputArr.Length; i++)
            {
                minHeap.insertKey(inputArr[i]);
            }

            minHeap.MinHeapify(0);
            Assert.True(ArrayTestUtils.ArraysEqual(minHeap.heapArr, expectedArr));
        }