Ejemplo n.º 1
0
        public void GetKeys()
        {
            int[] random         = ArrayUtility.GenerateRandomIntArray(5, 1000);
            int[] sortedDistinct = ArrayUtility.GetSortedDistinct(random);

            KevinDictionary <int, string> kd           = new KevinDictionary <int, string>(100);
            SortedList <int, int>         originalKeys = new SortedList <int, int>();

            for (int i = 0; i < sortedDistinct.Length; i++)
            {
                kd.Put(sortedDistinct[i], sortedDistinct[i].ToString());
                originalKeys.Add(sortedDistinct[i], sortedDistinct[i]);
            }

            int[] keysFromDictionary = kd.Keys.ToArray();
            int[] sdk = ArrayUtility.GetSortedDistinct(keysFromDictionary);
            Assert.IsTrue(ArrayUtility.AreIntegerEnumerablesEqual(originalKeys.Values.ToArray(), sdk));

            for (int i = 0; i < sortedDistinct.Length; i++)
            {
                kd.Remove(sortedDistinct[i]);
                originalKeys.Remove(sortedDistinct[i]);

                keysFromDictionary = kd.Keys.ToArray();
                sdk = ArrayUtility.GetSortedDistinct(keysFromDictionary);

                Assert.IsTrue(ArrayUtility.AreIntegerEnumerablesEqual(originalKeys.Values.ToArray(), sdk));
            }
        }
Ejemplo n.º 2
0
        public void HeapSortLargeRandomArray()
        {
            int[] inputArray = ArrayUtility.GenerateRandomIntArray(1000);

            int[] expectedArray = inputArray.Clone() as int[];
            // default sort puts min at the front of the sorted array
            Array.Sort(expectedArray);

            HeapSort.Sort(inputArray);
            Assert.IsTrue(ArrayUtility.AreIntegerEnumerablesEqual(inputArray, expectedArray));
        }
Ejemplo n.º 3
0
        public void QuickSortLargeRandomArray()
        {
            int[] inputArray = ArrayUtility.GenerateRandomIntArray(arrayLength: 1000, maxValue: 20);

            int[] expectedArray = inputArray.Clone() as int[];
            // default sort puts min at the front of the sorted array
            Array.Sort(expectedArray);

            QuickSort.Sort(inputArray, QuickSortCCI.Partition);
            Assert.IsTrue(ArrayUtility.AreIntegerEnumerablesEqual(inputArray, expectedArray));
        }
Ejemplo n.º 4
0
        public void MinHeapLargeRandom()
        {
            KevinMinHeap minHeap = null;

            int[]      inputArray    = null;
            int[]      expectedArray = null;
            List <int> poppedValues  = new List <int>();

            try
            {
                inputArray = ArrayUtility.GenerateRandomIntArray(1000);

                expectedArray = inputArray.Clone() as int[];
                // default sort puts min at the front of the sorted array
                Array.Sort(expectedArray);

                minHeap = (KevinMinHeap)HeapFactory.CreateMinHeap(inputArray);
                int expectedCount = inputArray.Length;
                Assert.AreEqual(minHeap.GetCount(), expectedCount);

                Console.WriteLine("binHeap:");
                Console.WriteLine(string.Join(" ", minHeap.ToString()));


                for (int i = 0; i < inputArray.Length; i++)
                {
                    int actual = minHeap.PopMin();
                    poppedValues.Add(actual);

                    int expected = expectedArray[i];
                    Assert.AreEqual(actual, expected);
                    expectedCount--;
                    Assert.AreEqual(minHeap.GetCount(), expectedCount);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("inputArray:");
                Console.WriteLine(string.Join(" ", inputArray));
                Console.WriteLine("expectedArray:");
                Console.WriteLine(string.Join(" ", expectedArray));
                Console.WriteLine("popped values:");
                Console.WriteLine(string.Join(" ", poppedValues));
                Console.WriteLine("minHeap:");
                Console.WriteLine(minHeap.ToString());

                throw;
            }
        }
Ejemplo n.º 5
0
        public void QuickSortComparePartitionArrayFuncs()
        {
            int[] inputArrayClassic = ArrayUtility.GenerateRandomIntArray(arrayLength: 1000, maxValue: 20);

            int[] inputArrayKevin = inputArrayClassic.Clone() as int[];

            QuickSort.Sort(inputArrayClassic, QuickSortClassicPartition.PartitionArray);
            Console.WriteLine("Classic partition stats");
            Console.WriteLine(QuickSort.stats);

            QuickSort.Sort(inputArrayKevin, QuickSortKevinPartition.PartitionArray);
            Console.WriteLine("Kevin partition stats");
            Console.WriteLine(QuickSort.stats);

            QuickSort.Sort(inputArrayKevin, QuickSortCCI.Partition);
            //Console.WriteLine("QuickSortCCI partition stats");
            //Console.WriteLine(QuickSort.stats);
        }
Ejemplo n.º 6
0
 public void LargeOddOptimal()
 {
     int[] a = ArrayUtility.GenerateRandomIntArray(101);
     Experiment.Sort.Sort.SortToPeaksAndValleysOptimal(a);
 }
Ejemplo n.º 7
0
 public void LengthThreeOptimal()
 {
     int[] a = ArrayUtility.GenerateRandomIntArray(3);
     Experiment.Sort.Sort.SortToPeaksAndValleysOptimal(a);
 }
Ejemplo n.º 8
0
 public void SingletonArrayOptimal()
 {
     int[] a = ArrayUtility.GenerateRandomIntArray(1);
     Experiment.Sort.Sort.SortToPeaksAndValleysOptimal(a);
     Assert.IsTrue(IsAllPeaksAndValleys(a));
 }
Ejemplo n.º 9
0
 public void LargeEven()
 {
     int[] a = ArrayUtility.GenerateRandomIntArray(100);
     Experiment.Sort.Sort.SortToPeaksAndValleys(a);
 }
Ejemplo n.º 10
0
 public void LengthTwo()
 {
     int[] a = ArrayUtility.GenerateRandomIntArray(2);
     Experiment.Sort.Sort.SortToPeaksAndValleys(a);
 }