Exemple #1
0
        /// <summary>
        /// Given an array of integers, return indices of the two numbers such that they add up to a specific target.

        //  You may assume that each input would have exactly one solution, and you may not use the same element twice.
        //  Example:
        //  Given nums = [2, 7, 11, 15], target = 9,
        //  Because nums[0] + nums[1] = 2 + 7 = 9,
        //  return [0, 1].
        //  </summary>
        //  <param name="nums"></param>
        //  <param name="target"></param>
        //  <returns></returns>
        //Problem with this solution is, it won't keep the original array indices intact
        public static bool CalculateTwoSumUsingSortingArray(int[] nums, int target)
        {
            bool result = false;

            int left  = 0;
            int right = nums.Length - 1;

            QuickSorting.QuickSort(nums, left, right);

            while (left < right)
            {
                if (nums[left] + nums[right] == target)
                {
                    result = true;
                    break;
                }
                else if (nums[left] + nums[right] <= target)
                {
                    left++;
                }
                else
                {
                    right--;
                }
            }

            return(result);
        }
Exemple #2
0
        public void TestRandomArray()
        {
            bool test = true;

            int[] array = new int[1000];
            var   rand  = new Random();

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(0, 999);
            }
            QuickSorting.QuickSort(array, 0, array.Length - 1);

            /* полная проверка всего массива
             * for (int i = 0; i < array.Length - 1; i++)
             * {
             *  if (array[i] > array[i + 1])
             *  {
             *      test = false;
             *      break;
             *  }
             * }*/
            for (int i = 0; i < 10; i++)
            {
                var a = rand.Next(0, 998);
                if (array[a] > array[a + 1])
                {
                    test = false;
                    break;
                }
            }
            Assert.IsTrue(test);
        }
Exemple #3
0
        public void TestLitleArray()
        {
            bool test = true;

            int[] array = new[] { 4, 8, 3 };
            QuickSorting.QuickSort(array, 0, array.Length - 1);
            if (array[0] > array[1] || array[1] > array[2])
            {
                test = false;
            }
            Assert.IsTrue(test);
        }
Exemple #4
0
        public void TestEmptyArray()
        {
            bool test = true;

            int[] array = new int[0];
            QuickSorting.QuickSort(array, 0, array.Length - 1);
            for (int i = 0; i < array.Length - 1; i++)
            {
                if (array[i] > array[i + 1])
                {
                    test = false;
                    break;
                }
            }
            Assert.IsTrue(test);
        }
Exemple #5
0
        public void TestSameNumberArray()
        {
            bool test = true;

            int[] array = new int [100];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = 1;
            }
            QuickSorting.QuickSort(array, 0, array.Length - 1);
            if (array[0] > array[array.Length - 1])
            {
                test = false;
            }
            Assert.IsTrue(test);
        }
Exemple #6
0
        public void TestBigArray()
        {
            bool test = true;

            int[] array = new int[150000000]; // 4гб оперативной памяти, процессор i3
            var   rand  = new Random();

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(0, 1499999);
            }
            QuickSorting.QuickSort(array, 0, array.Length - 1);
            for (int i = 0; i < array.Length - 1; i++)
            {
                if (array[i] > array[i + 1])
                {
                    test = false;
                    break;
                }
            }
            Assert.IsTrue(test);
        }