コード例 #1
0
ファイル: Program.cs プロジェクト: diophung/common-problems
        public static void SortAlgos()
        {
            int[] A = new int[] { 3, 5, 6, 7, 2, 2, 4, 5, 19, 24, 6, 19, 43, 28 };

            Console.WriteLine("\nInsertionSort");
            SortAlgo sortAlgo = new SortAlgo();

            foreach (var i in sortAlgo.InsertionSort(A))
            {
                Console.Write("{0}\t", i);
            }

            Console.WriteLine("\nBubbleSort");
            A = new int[] { 3, 5, 6, 7, 2, 2, 4, 5, 19, 24, 6, 19, 43, 28 };
            foreach (var i in sortAlgo.BubleSort(A))
            {
                Console.Write("{0}\t", i);
            }

            A = new int[] { 3, 5, 6, 7, 2, 2, 4, 5, 19, 24, 6, 19, 43, 28 };
            sortAlgo.QuickSort(A, 0, A.Length - 1);
            Console.WriteLine("\nQuickSort");
            foreach (var i in A)
            {
                Console.Write("{0}\t", i);
            }

            Console.WriteLine("\nMergeSort");
            A = new int[] { 3, 5, 6, 7, 2, 2, 4, 5, 19, 24, 6, 19, 43, 28 };
            sortAlgo.MergeSort(A, 0, A.Length - 1);
            foreach (var i in A)
            {
                Console.Write("{0}\t", i);
            }
        }
コード例 #2
0
        //problem: given a int[] in sorted ascending order,
        //output the int[] where x' = A*x^2 + B*x + C, also in sorted ascending order
        //To be achieved in O(n)
        public static int[] MapArrayFn(int[] input, int A, int B, int C)
        {
            int[] mapped = new int[input.Length];

            for (int i = 0; i < input.Length; i++)
            {
                mapped[i] = MapFunction(input[i], A, B, C);
            }

            var sortAlgo = new SortAlgo();

            sortAlgo.QuickSort(mapped, 0, mapped.Length - 1);

            return(mapped);
        }
コード例 #3
0
        int _binarySearch(T[] array, T item)
        {
            int      i = 0, j = _count - 1;
            List <P> mlist = new List <P>();

            for (int ii = 0; ii < _count; ii++)
            {
                P p = new P()
                {
                    Value = _data[ii], Index = ii
                };
                mlist.Add(p);
            }

            var temp = mlist.ToArray();

            SortAlgo <P> .QuickSort(temp);

            P value = new P()
            {
                Value = item, Index = -1
            };

            while (i <= j)
            {
                int M       = (i + j) / 2;
                int compare = temp[M].CompareTo(value);
                if (compare == 0)
                {
                    return(temp[M].Index);
                }
                else if (compare > 0)
                {
                    j = M - 1;
                }
                else
                {
                    i = M + 1;
                }
            }
            return(-1);
        }
コード例 #4
0
ファイル: SortTests.cs プロジェクト: diophung/common-problems
        public void TestSortAlgos()
        {
            SortAlgo sortAlgo     = new SortAlgo();
            var      bubbleSorted = sortAlgo.BubleSort(_arr);

            Assert.IsTrue(_sortedArr.ElementEquals(bubbleSorted));

            var insertionSort = sortAlgo.InsertionSort(_arr);

            Assert.IsTrue(_sortedArr.ElementEquals(insertionSort));

            var clone = new int[] { 3, 5, 6, 7, 2, 2, 19, 43, 28 };

            sortAlgo.QuickSort(clone, 0, _arr.Length - 1);
            Assert.IsTrue(clone.ElementEquals(_sortedArr));

            var quickSortClone = new int[] { 3, 5, 6, 7, 2, 2, 19, 43, 28 };

            sortAlgo.MergeSort(quickSortClone, 0, quickSortClone.Length - 1);
            Assert.IsTrue(quickSortClone.ElementEquals(_sortedArr));
        }
コード例 #5
0
 public void Sort() => SortAlgo <T> .MergeSort(_data, 0, _count - 1, (a, b) => a.CompareTo(b));