Ejemplo n.º 1
0
        /// <summary>
        /// time complexity  - average - O(n log(n)); best - O(n); worst - O(n^2)
        /// space complexity - O(n)
        /// </summary>
        public static int QuickSelectionSort(int[] array, int k, int fromIndex, int toIndex)
        {
            //Partitioning
            var swapPointer = fromIndex;

            for (int i = fromIndex; i <= toIndex; i++)
            {
                if (array[i] < array[toIndex])
                {
                    CommonArrayFunctions.SwapElements(array, swapPointer, i);
                    swapPointer++;
                }
            }

            CommonArrayFunctions.SwapElements(array, swapPointer, toIndex);

            if (swapPointer > k)
            {
                return(QuickSelectionSort(array, k, fromIndex, swapPointer - 1));
            }
            if (swapPointer < k)
            {
                return(QuickSelectionSort(array, k, swapPointer + 1, toIndex));
            }

            return(array[k]);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// time complexity  - average - O(n^2); best - O(n); worst - O(n^2)
        /// space complexity - O(1)
        /// </summary>
        public static void ShakerSort(int[] array)
        {
            for (var i = 0; i < array.Length / 2; i++)
            {
                var swapFlag = false;

                for (var j = i; j < array.Length - i - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        CommonArrayFunctions.SwapElements(array, j, j + 1);
                        swapFlag = true;
                    }
                }

                for (var j = array.Length - 2 - i; j > i; j--)
                {
                    if (array[j - 1] > array[j])
                    {
                        CommonArrayFunctions.SwapElements(array, j - 1, j);
                        swapFlag = true;
                    }
                }

                if (!swapFlag)
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// time complexity  - O(n)
        /// space complexity - O(1)
        /// </summary>
        public static void DutchProblemSort(int[] array)
        {
            int lowIndex = 0, midIndex = 0;
            int hiIndex = array.Length - 1;

            while (midIndex <= hiIndex)
            {
                switch (array[midIndex])
                {
                case 0:
                    CommonArrayFunctions.SwapElements(array, midIndex, lowIndex);
                    lowIndex++;
                    midIndex++;
                    break;

                case 1:
                    midIndex++;
                    break;

                case 2:
                    CommonArrayFunctions.SwapElements(array, midIndex, hiIndex);
                    hiIndex--;
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// time complexity  - average - O(n log(n)); best - O(n log(n)); worst - O(n^2)
        /// space complexity - O(n)
        /// </summary>
        public static void QuickSort(int[] array, int fromIndex, int toIndex)
        {
            var subArrayLength = toIndex - fromIndex;

            if (subArrayLength < 1)
            {
                return;
            }

            var pivotIndex = fromIndex + subArrayLength / 2;

            CommonArrayFunctions.SwapElements(array, pivotIndex, toIndex);

            var swapPointer = fromIndex;

            for (int i = fromIndex; i < toIndex; i++)
            {
                if (array[i] < array[toIndex])
                {
                    CommonArrayFunctions.SwapElements(array, swapPointer, i);
                    swapPointer++;
                }
            }

            CommonArrayFunctions.SwapElements(array, swapPointer, toIndex);

            QuickSort(array, fromIndex, swapPointer - 1);
            QuickSort(array, swapPointer + 1, toIndex);
        }
Ejemplo n.º 5
0
        private void TestSort(Action <int[]> sortMethod)
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var array       = CommonArrayFunctions.InitIntArray(i / 100 + 1, 0, random.Next(0, i));
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);

                sortMethod(sortedArray);

                Assert.IsTrue(IsArraysEqual(sortedArray, array.OrderBy(x => x).ToArray()));
            }
        }
Ejemplo n.º 6
0
        public void DutchProblemSortTest()
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var array       = CommonArrayFunctions.InitIntArray(i / 100 + 1, 0, 2);
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);

                SortArray.DutchProblemSort(sortedArray);

                Assert.IsTrue(IsArraysEqual(sortedArray, array.OrderBy(x => x).ToArray()));
            }
        }
Ejemplo n.º 7
0
        public void QuickSelectionSortTest()
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var array       = CommonArrayFunctions.InitIntArray(i / 100 + 1, 0, random.Next(0, i));
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);
                var kIndex = random.Next(0, sortedArray.Length - 1);

                var kValue = SortArray.QuickSelectionSort(sortedArray, kIndex, 0, sortedArray.Length - 1);

                Assert.AreEqual(array.OrderBy(x => x).ElementAt(kIndex), kValue);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// time complexity  - O(n^2)
        /// space complexity - O(1)
        /// </summary>
        public static void BubbleSort(int[] array)
        {
            bool?swapped = null;

            while (swapped != false)
            {
                swapped = false;
                for (int i = 1; i < array.Length; i++)
                {
                    if (array[i] < array[i - 1])
                    {
                        CommonArrayFunctions.SwapElements(array, i, i - 1);
                        swapped = true;
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void TestBinarySearch()
        {
            var random = new Random();

            for (int t = 0; t < 100000; t++)
            {
                var array       = CommonArrayFunctions.InitIntArray(random.Next(50, 150), 0, random.Next(50, 150));
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);
                SortArray.SelectionSort(sortedArray);

                for (int t2 = 0; t2 < array.Length; t2++)
                {
                    var result = SearchInArray.BinarySearchRecursive(sortedArray, 0, array.Length, array[t2]);

                    Assert.IsTrue(array[t2] == sortedArray[result]);
                }
            }
        }