Esempio n. 1
0
        public void Quicksort_Int_TestOnArrayWithOneElement()
        {
            var array = new int[] { 1 };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Esempio n. 2
0
        public void Quicksort_Int_TestOnUnsortedArrayWithDuplicates()
        {
            var array = new int[] { 0, 3, 9, 7, 1, 4, 5, 2, 7, 8, 6, 3, 1 };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Esempio n. 3
0
        public void Quicksort_Int_TestOnSortedArray()
        {
            var array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Esempio n. 4
0
        public void Quicksort_String_TestOnUnsortedArrayWithDuplicates()
        {
            var array = new string[] { "f", "b", "g", "a", "d", "e", "a", "c", "b" };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Esempio n. 5
0
        public void Quicksort_String_TestOnSortedArray()
        {
            var array = new string[] { "a", "b", "c", "d", "e", "f", "g" };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
        static void SortingScenario()
        {
            Selection sort  = new Selection();
            var       array = sort.InitialiseArray();

            Console.Write("Unsorted Array ");
            Console.Write("\n");
            for (int k = 0; k < array.Count; k++)
            {
                Console.Write(array[k] + " ");
            }
            Console.Write("\n");
            Console.WriteLine("**********Selection Sort**************************");
            var sorted = sort.Sort(sort.InitialiseArray());

            Console.WriteLine("**********End Selection Sort**************************");

            Console.WriteLine("**********Insert Sort**************************");
            Insert sort2   = new Insert();
            var    sorted2 = sort2.Sort(sort2.InitialiseArray());

            Console.WriteLine("**********End Insert Sort**************************");

            Console.WriteLine("**********Quick Sort**************************");
            Quick quick   = new Quick();
            var   sorted3 = quick.Sort(sort2.InitialiseArray(), 0, sort2.InitialiseArray().Count - 1);

            Console.WriteLine("**********End Quick Sort**************************");
            Console.ReadLine();
        }
Esempio n. 7
0
        public void Quicksort_Int_TestOnEmptyArray()
        {
            var array = new int[] { };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Esempio n. 8
0
        public static int Solve(long[] arr)
        {
            var leftBound  = new long[arr.Length];
            var rightBound = new long[arr.Length];

            for (var i = 0; i < arr.Length; i++)
            {
                rightBound[i] = i + arr[i];
                leftBound[i]  = i - arr[i];
            }

            Quick.Sort(leftBound);
            Quick.Sort(rightBound);

            var count = 0;
            var j     = 0;

            for (var i = 0; i < arr.Length; i++)
            {
                while (j < arr.Length && rightBound[i] >= leftBound[j])
                {
                    count += j - i;
                    j++;
                    if (count > 1e7)
                    {
                        return(-1);
                    }
                }
            }

            return(count);
        }
Esempio n. 9
0
        public void Quicksort_Int_TestOnReverseSortedArray()
        {
            var array = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

            Quick.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Esempio n. 10
0
        public void SortTest(int[] input, int[] expected)
        {
            // Arrange & Act
            Quick.Sort(input);

            // Assert
            CollectionAssert.AreEqual(expected, input);
        }
Esempio n. 11
0
        public void TestQuickSortOdd()
        {
            var inputArr    = new [] { 3, 7, 8, 0, 2, 6, 7 };
            var expectedArr = new[] { 0, 2, 3, 6, 7, 7, 8 };

            Quick.Sort(inputArr);
            Assert.That(inputArr, Is.EqualTo(expectedArr));
        }
Esempio n. 12
0
        public void QuickSortTest()
        {
            string inputText    = "befdac";
            ISort  sortingStrat = new Quick();
            string outputText   = sortingStrat.Sort(inputText);

            Assert.IsTrue(outputText == "abcdef");
        }
Esempio n. 13
0
        public void NetQuickSort5()
        {
            Quick      q      = new Quick();
            List <int> sorted = q.Sort(sortList5);

            Console.WriteLine("Count of iterations:" + q.iterCount);
            Assert.AreEqual(sorted, new List <int>(sorted5));
        }
Esempio n. 14
0
        public void QuickSort()
        {
            var array = new int[] { 6, 5, 4, 3, 212, 1, 32, 2 };

            Quick.Sort(array);

            Assert.Equal(new int[] { 1, 2, 3, 4, 5, 6, 32, 212 }, array);
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            int[] nums  = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var   quick = new Quick <int>();

            quick.Sort(nums);
            Console.WriteLine(string.Join(" ", nums));
        }
Esempio n. 16
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_Elementary_IsSorted()
        {
            IComparable[] unsortedArray = "4321".Select(c => c.ToString()).ToArray();
            IComparable[] expectedArray = "1234".Select(c => c.ToString()).ToArray();

            Quick.Sort(unsortedArray);

            CollectionAssert.AreEqual(expectedArray, unsortedArray);
        }
Esempio n. 17
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_Words3_IsSorted()
        {
            IComparable[] unsortedArray = "bed bug dad yes zoo now for tip ilk dim tag jot sob nob sky hut men egg few jay owl joy rap gig wee was wad fee tap tar dug jam all bad yet".Split(' ');
            IComparable[] expectedArray = "all bad bed bug dad dim dug egg fee few for gig hut ilk jam jay jot joy men nob now owl rap sky sob tag tap tar tip wad was wee yes yet zoo".Split(' ');

            Quick.Sort(unsortedArray);

            CollectionAssert.AreEqual(expectedArray, unsortedArray);
        }
Esempio n. 18
0
        static void Main(string[] args)
        {
            var array = new int[] { 5, 1, 3, 8, 4, 9, 2, 6 };

            Quick.Sort(array);
            Print(array);

            Console.ReadLine();
        }
Esempio n. 19
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_Sorted_IsSorted()
        {
            IComparable[] unsortedArray = "AEELMOPRSTX".Select(c => c.ToString()).ToArray();
            IComparable[] expectedArray = "AEELMOPRSTX".Select(c => c.ToString()).ToArray();

            Quick.Sort(unsortedArray);

            CollectionAssert.AreEqual(expectedArray, unsortedArray);
        }
Esempio n. 20
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_TimeTable_IsStable()
        {
            IEnumerable <TimeTable> sortedSample = TimeTable.GetTestSample();

            TimeTable[] stableByQuickSortSample = TimeTable.GetTestSample().ToArray();

            //rearrange an array of objects in uniformly random order
            Knuth.Shuffle <TimeTable>(stableByQuickSortSample);

            //sort by TIME
            Quick.Sort(stableByQuickSortSample, new TimeTableComparerByTime());
            //City: Chicago, Time:09:00:00
            //City: Phoenix, Time:09:00:03
            //City: Houston, Time:09:00:13
            //City: Chicago, Time:09:00:59
            //City: Houston, Time:09:01:10
            //City: Chicago, Time:09:03:13
            //City: Seattle, Time:09:10:11
            //City: Seattle, Time:09:10:25
            //City: Phoenix, Time:09:14:25
            //City: Chicago, Time:09:19:32
            //City: Chicago, Time:09:19:46
            //City: Chicago, Time:09:21:05
            //City: Seattle, Time:09:22:43
            //City: Seattle, Time:09:22:54
            //City: Chicago, Time:09:25:52
            //City: Chicago, Time:09:35:21
            //City: Seattle, Time:09:36:14
            //City: Phoenix, Time:09:37:44

            //sort by CITY
            Quick.Sort(stableByQuickSortSample, new TimeTableComparerByCity());

            //algorithms is STABLE if sorted by time is preserved for the same city
            //City: Chicago, Time:09:00:00
            //City: Chicago, Time:09:00:59
            //City: Chicago, Time:09:03:13
            //City: Chicago, Time:09:19:32
            //City: Chicago, Time:09:19:46
            //City: Chicago, Time:09:21:05
            //City: Chicago, Time:09:25:52
            //City: Chicago, Time:09:35:21
            //City: Houston, Time:09:00:13
            //City: Houston, Time:09:01:10
            //City: Phoenix, Time:09:00:03
            //City: Phoenix, Time:09:14:25
            //City: Phoenix, Time:09:37:44
            //City: Seattle, Time:09:10:11
            //City: Seattle, Time:09:10:25
            //City: Seattle, Time:09:22:43
            //City: Seattle, Time:09:22:54
            //City: Seattle, Time:09:36:14

            CollectionAssert.AreNotEqual(sortedSample, stableByQuickSortSample);
            CollectionAssert.AreEquivalent(sortedSample, stableByQuickSortSample);
        }
Esempio n. 21
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_NullItem_IsSorted()
        {
            IComparable[] unsortedArray = new string[0];

            IComparable[] expectedArray = new string[0];

            Quick.Sort(unsortedArray);

            CollectionAssert.AreEqual(expectedArray, unsortedArray);
        }
Esempio n. 22
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_EmptyItems_IsSorted()
        {
            IComparable[] unsortedArray = Enumerable.Range(1, 10).Select(x => string.Empty).ToArray();

            IComparable[] expectedArray = Enumerable.Range(1, 10).Select(x => string.Empty).ToArray();

            Quick.Sort(unsortedArray);

            CollectionAssert.AreEqual(expectedArray, unsortedArray);
        }
Esempio n. 23
0
        private void SortGridArray()
        {
            var starttime = DateTime.Now;

            Quick.Sort(gridArray);
            var stoptime = DateTime.Now;

            var intDuration = stoptime - starttime;

            Console.WriteLine($"Sorting the data took: {intDuration}");
        }
Esempio n. 24
0
        void InternalTest(int[] nums1, int[] nums2, int[] expected)
        {
            int[] actual = IntersectionOfTwoArraysII.Intersect(nums1, nums2);
            Assert.Equal <int>(expected.Length, actual.Length);

            //the order does not matter
            Quick.Sort(expected);
            Quick.Sort(actual);

            for (int i = 0; i < actual.Length; i++)
            {
                Assert.Equal <int>(expected[i], actual[i]);
            }
        }
Esempio n. 25
0
        public void TestQuickSort()
        {
            var a = new int[16];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(100);
            }
            StdOut.WriteLine(a);
            Quick <int> .Sort(a);

            StdOut.WriteLine(a);

            var item4 = Quick <int> .Select(a, 9);

            StdOut.WriteLine("item7 = {0}", item4);
        }
Esempio n. 26
0
        public static int Solve(int[] a)
        {
            var sortedA            = Quick.Sort(a);
            var max                = int.MinValue;
            var maxNegativeProduct = int.MinValue;

            for (var i = 2; i < sortedA.Length; i++)
            {
                if (sortedA[i - 1] < 0)
                {
                    maxNegativeProduct = Math.Max(maxNegativeProduct, GetMaxNegativePair(sortedA, i));
                }

                max = Math.Max(max, TripletProduct(sortedA, i, maxNegativeProduct));
            }
            return(max);
        }
Esempio n. 27
0
        public static int Solve(int[] a)
        {
            if (a.Length < 2)
            {
                return(0);
            }

            var sortedA = Quick.Sort(a);

            for (var i = 2; i < sortedA.Length; i++)
            {
                if (IsTriangularTriplet(sortedA[i - 2], sortedA[i - 1], sortedA[i]))
                {
                    return(1);
                }
            }
            return(0);
        }
Esempio n. 28
0
        static void Main(string[] args)
        {
            string input = Console.ReadLine();

            if (input == String.Empty)
            {
                return;
            }

            int[] arr = input
                        .Split()
                        .Select(int.Parse)
                        .ToArray();

            Quick.Sort(arr);

            Console.WriteLine(String.Join(" ", arr));
        }
Esempio n. 29
0
        //[MethodName_StateUnderTest_ExpectedBehavior]
        public void Sort_EmptyAndNonEpmtyItems_IsSorted()
        {
            var empty   = Enumerable.Range(1, 10).Select(x => string.Empty).ToArray();;
            var newGuid = Guid.NewGuid().ToString().Select(c => c.ToString());

            var newGuidSorted = newGuid.ToArray();

            Array.Sort(newGuidSorted);

            IComparable[] unsortedArray = newGuid.Concat(empty).ToArray();
            IComparable[] expectedArray = empty.Concat(newGuidSorted).ToArray();

            Array.Sort(newGuidSorted);

            Quick.Sort(unsortedArray);

            CollectionAssert.AreEqual(expectedArray, unsortedArray);
        }
Esempio n. 30
0
        public void QuickTest1()
        {
            string[] a = new string[] { "aba" };
            string   s;

            Quick.Sort(a);
            s = (string)Quick.Select(a, 0);
            Assert.AreEqual(s, a[0]);

            a = new string[] { "zoo", "able", "after", "cury", "aba", "bed", "bug", "boy", "bing", " " };
            s = (string)Quick.Select(a, a.Length - 1);
            Assert.AreEqual(s, "zoo");

            Quick.Sort(a);
            Assert.AreEqual("aba", a[1]);

            Quick.Select(a, a.Length); // generate exception
        }