Exemple #1
0
        public void Sort_PassValidArray_ReturnSortedArray()
        {
            var quickSorting = new QuickSorting();
            var sortedArray  = quickSorting.Sort(_toSortArray, 0, _toSortArray.Length - 1);

            CollectionAssert.AreEqual(sortedArray, _referenceSortedArray);
        }
Exemple #2
0
        public void Sort_PassEmptyArray_ReturnEmptyArray()
        {
            var quickSorting = new QuickSorting();
            var sortedArray  = quickSorting.Sort(_emptyArray, 0, _emptyArray.Length - 1);

            CollectionAssert.AreEqual(sortedArray, _emptyArray);
        }
Exemple #3
0
        public void Sort_PassOneElementArray_ReturnOneElementArray()
        {
            var quickSorting = new QuickSorting();
            var sortedArray  = quickSorting.Sort(_oneElementArray, 0, _oneElementArray.Length - 1);

            CollectionAssert.AreEqual(sortedArray, _oneElementArray);
        }
Exemple #4
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 #5
0
        public static double[] SortStrings(string[] arr, long left, long right)
        {
            double[] nums = null;

            try
            {
                nums = new double[arr.Length];
                for (int i = 0; i < arr.Length; i++)
                {
                    if (double.TryParse(arr[i], out nums[i]))
                    {
                        nums[i] = double.Parse(arr[i]);
                    }
                    else
                    {
                        throw new QuickSortingException("Not all values could be converted to numbers");
                    }
                }

                QuickSorting.Sort(nums, 0, nums.Length - 1);
            }
            catch (QuickSortingException e)
            {
                Console.WriteLine(e.ToString());
            }

            return(nums);
        }
 private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (SortSelect.SelectedIndex == 0)
     {
         TableList.Clear();
         CreateTable();
     }
     if (SortSelect.SelectedIndex == 1)
     {
         QuickSorting.sortingRKK(TableList, 0, TableList.Count - 1);
         TableListReverse();
     }
     if (SortSelect.SelectedIndex == 2)
     {
         QuickSorting.sortingAppeal(TableList, 0, TableList.Count - 1);
         TableListReverse();
     }
     if (SortSelect.SelectedIndex == 3)
     {
         QuickSorting.sortingSum(TableList, 0, TableList.Count - 1);
         TableListReverse();
     }
     for (int i = 0; i < TableList.Count; i++)
     {
         TableList[i].Id = i + 1;
     }
 }
Exemple #7
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 #8
0
 public static void Sort(NSJSArray s, int low, int high, Func <NSJSValue, NSJSValue, bool> max)
 {
     if (!(s.Length <= 1 || low >= high))
     {
         int middle = QuickSorting.Middle(s, low, high, max);
         QuickSorting.Sort(s, low, middle - 1, max);  // 向左向中心扩散
         QuickSorting.Sort(s, middle + 1, high, max); // 从中心向右扩散
     }
 }
Exemple #9
0
        public void QuickSortingUnitTest_Sort__SuccessResult()
        {
            ISorting testClass = new QuickSorting();

            int[] array = new int[] { 1, 0, 2, 9, 3, 8, 4, 7, 5, 6 };
            testClass.Sort(array);

            array.Should().Equal(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
        }
Exemple #10
0
        public void QuickSorting_QuicksortParallelTest()
        {
            QuickSorting sorting = new QuickSorting();

            int[] testArray = new[] { 3, 2, 7, 3, 1, 4, 5, 4 };

            sorting.QuicksortParallel(testArray, 2, 5);

            CollectionAssert.AreEqual(testArray, new int[] { 3, 2, 1, 3, 4, 7, 5, 4 });
        }
Exemple #11
0
        public void QuickSorting_PartitionTest()
        {
            QuickSorting sorting = new QuickSorting();

            int[] testArray = new[] { 3, 2, 7, 3, 1, 4, 5, 4 };

            sorting.Partition(testArray, 2, 5);

            CollectionAssert.AreEqual(testArray, new int[] { 3, 2, 1, 3, 7, 4, 5, 4 });
        }
Exemple #12
0
        public void QuickSorting_SortTest()
        {
            QuickSorting sorting = new QuickSorting();

            sorting.Array = new[] { 3, 2, 7, 3, 1 };
            sorting.N     = sorting.Array.Length;

            sorting.Sort();

            CollectionAssert.AreEqual(sorting.Array, new int[] { 1, 2, 3, 3, 7 });
        }
Exemple #13
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 #14
0
        public void TestMethod1()
        {
            int[] expected = new int[5] {
                3, 4, 5, 6, 7
            };
            int[] array = new int[5] {
                5, 3, 6, 7, 4
            };

            QuickSorting a = new QuickSorting();

            int[] actual = QuickSorting.Sorting(array, 0, array.Length - 1);
            CollectionAssert.AreEqual(expected, actual);
        }
Exemple #15
0
        public void QuickSortingHoar_Test()
        {
            var rand = new Random();

            var list = Enumerable
                       .Range(1, 10000)
                       .Select(x => rand.Next())
                       .ToList();

            var actual = QuickSorting.Sort(list, PartitionType.Hoar);

            list.Sort();

            Assert.Equal(list, actual);
        }
Exemple #16
0
        public void QuickSorting_SwapTest()
        {
            QuickSorting sorting = new QuickSorting();

            int[] testArray = new[] { 3, 2, 7, 3, 1, 4, 5, 4 };
            int   i         = 2;
            int   j         = 5;
            int   old_i     = testArray[i];
            int   old_j     = testArray[j];

            sorting.Swap(testArray, i, j);

            Assert.AreEqual(testArray[i], old_j);
            Assert.AreEqual(testArray[j], old_i);
        }
Exemple #17
0
            public static void Sort <T>(IList <T> nums, int low, int high, Func <T, T, bool> max)
            {
                if (nums == null || max == null)
                {
                    throw new ArgumentNullException();
                }
                if (nums.Count <= 1 || low >= high)
                {
                    return;
                }
                int middle = Middle(nums, low, high, max);

                QuickSorting.Sort(nums, low, middle - 1, max);
                QuickSorting.Sort(nums, middle + 1, high, max);
            }
Exemple #18
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 #19
0
        public void QuickSorting_FullTest()
        {
            QuickSorting sorting = new QuickSorting();

            sorting.SetGenerateStrategy(new DescSortedGenerateStrategy());

            sorting.TestSortingAlgorithm();

            int[] expected = new int[sorting.N];
            for (int i = 0; i < sorting.N; i++)
            {
                expected[i] = i + 1;
            }

            CollectionAssert.AreEqual(sorting.Array, expected);
        }
Exemple #20
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 #21
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);
        }
Exemple #22
0
 public QuickSortController(IDataService dservice, QuickSorting sorting)
 {
     this.db      = dservice;
     this.sorting = sorting;
 }
Exemple #23
0
 private static void Quick(IntPtr info)
 {
     Sortable.Sort(info, (s, low, high, max) => QuickSorting.Sort(s, low, high, max));
 }
Exemple #24
0
 public static void Quick <T>(IList <T> nums, int low, int high, Func <T, T, bool> max)
 {
     QuickSorting.Sort(nums, low, high, max);
 }
Exemple #25
0
 public static void Sort <T>(IList <T> nums, Func <T, T, bool> max)
 {
     QuickSorting.Sort <T>(nums, 0, nums.Count - 1, max);
 }
Exemple #26
0
 static void Main()
 {
     SelectionSorting.SelectionSortArray();
     InsertionSorting.InsertionSortArray();
     QuickSorting.QuickSortArray();
 }
Exemple #27
0
 public static void Quick <T>(IList <T> nums, Func <T, T, bool> max)
 {
     QuickSorting.Sort(nums, max);
 }
Exemple #28
0
        public void QuickSorting_Sort()
        {
            ISorting testClass = new QuickSorting();

            testClass.Sort(SortedData);
        }