Пример #1
0
        static void Main(string[] args)
        {
            var array = CreateArray();

            array = QuickSort.Sort(array);
            PrintArray(array);
        }
Пример #2
0
        public static void Test(int length, int minimumVal, int maximumVal)
        {
            QuickSort qSort = new QuickSort();
            int[] array = Utilities.GenerateRandomizedArray(length, minimumVal, maximumVal);
            int[] copy = new int[array.Length];
            array.CopyTo(copy, 0);

            Stopwatch watch = new Stopwatch();
            watch.Start();
            qSort.Sort(ref array, 0, array.Length - 1);
            watch.Stop();
            TimeSpan quickSortTime = watch.Elapsed;

            List<int> comparisonList = copy.ToList();
            watch.Restart();
            comparisonList.Sort();
            watch.Stop();
            TimeSpan dotNETTime = watch.Elapsed;

            Console.WriteLine(string.Format("Unsorted: {0}{1}Sorted: {2}{1}Elapsed Time: {3}{1}Compare To: {4}{1}",
                Utilities.FormatPrintArray(copy),
                Environment.NewLine,
                Utilities.FormatPrintArray(array),
                quickSortTime.ToString(),
                dotNETTime.ToString()));
        }
        static void Main(string[] args)
        {
            string strInput = "Tip for the future: you already knew that this was called the console, because you used that word in the question. So looking for 'C# console read text' on Google would be a good way to answer this question yourself. (Note: this is not flaming, just some feedback for the next question)";

            //QuestionCount(strInput);
            //List<string> strWords = new list<string>();
            string[] strWords;
            strInput = strInput.ToLower();
            strInput = strInput.Replace(".", string.Empty);

            strWords = strInput.Split(null);
            int count = 0;

            foreach (string str in strWords)
            {
                if (str == "question")
                {
                    count++;
                }
            }

            //BubbleSort _bubblesort = new BubbleSort();
            //InsertionSort _bubblesort = new InsertionSort();
            //SelectionSort _bubblesort = new SelectionSort();
            QuickSort _quicksort = new QuickSort();

            int[] aftersort = _quicksort.Sort(0, 8);
            //int[] aftersort = _bubblesort.Sort();
            for (int i = 0; i < aftersort.Length; i++)
            {
                Console.WriteLine(aftersort[i]);
            }
            Console.ReadLine();
        }
Пример #4
0
        private static void QuickSortTest()
        {
            Console.WriteLine("Quick Sort Test");

            int[] arr = new int[_arr.Length];

            Array.Copy(_arr, arr, _arr.Length);

            ISortAble quickSort = new QuickSort();

            quickSort.StartTimer();

            int[] sorted = quickSort.Sort(arr);

            quickSort.StopTimer();

            if (_printArray)
            {
                quickSort.PrintArray(arr);
            }

            quickSort.PrintElapsedTime();

            quickSort.ResetTimer();
        }
Пример #5
0
        public void QuickSortSortsArray()
        {
            var testCollection = TestUtils.GetUnsortedCharArray();
            var algorithm      = new QuickSort();

            algorithm.Sort(testCollection);
            Assert.True(TestUtils.IsSorted(testCollection));
        }
Пример #6
0
        private static void ExecuteQuickSort()
        {
            var array = new int[] { 7, 2, 17, 9, 1, 3, 3 };

            Console.WriteLine($"Quick sort - {string.Join(",", array)}");
            var sorter = new QuickSort();

            sorter.Sort(array);
            Console.WriteLine($"Sorted array - {string.Join(",", array)}");
        }
Пример #7
0
        public static void Execute()
        {
            var a = new int[] { 3, 2, 4, 5, 1, 6 };

            Common <int> .DisplayArray(a);

            var qs = new QuickSort <int>();

            qs.Sort(a, 0, a.Length - 1);
            Common <int> .DisplayArray(a);
        }
Пример #8
0
        public static KeyValuePair<bool, System.Windows.Point> SubOptimalTwoSumSolution(int[] array, int targetSum)
        {
            int[] sortedArray = array;
            bool sumExists = false;
            int a = int.MinValue;
            int remainder = int.MinValue;
            System.Windows.Point summands = new System.Windows.Point();

            //sort input array
            QuickSort qs = new QuickSort();
            qs.Sort(ref sortedArray, 0, sortedArray.Length - 1);

            for (int i = 0; i < sortedArray.Length; i++)
            {
                a = sortedArray[i];
                remainder = targetSum - sortedArray[i];
                for (int j = i + 1; j < sortedArray.Length; j++)
                {
                    int current = sortedArray[j];
                    if (current < remainder)
                    {
                        continue;
                    }
                    else if (current == remainder)
                    {
                        sumExists = true;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }

                if (sumExists)
                {
                    summands = new System.Windows.Point(a, remainder);
                    break;
                }
            }

            return new KeyValuePair<bool, System.Windows.Point>(sumExists, summands);
        }
Пример #9
0
        private static void TestSort()
        {
            var arr = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

            BinarySort.Sort(arr);

            for (var i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]);
            }
            Console.WriteLine();

            arr = new int[] { 3, 2, 1, 9, 8, 7, 6, 5, 4 };

            QuickSort.Sort(arr);

            for (var i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]);
            }
            Console.WriteLine();
        }
Пример #10
0
        public static void TestSelect()
        {
            Random           rand     = new Random();
            int              numTests = 5;
            int              nfrom    = -99999;
            int              nto      = 99999;
            RandomizedSelect select   = new RandomizedSelect();
            QuickSort        sort     = new QuickSort();

            Console.WriteLine(String.Format("****Testing {0}****", select.GetName()));
            for (int i = 0; i < numTests; i++)
            {
                int   arraysize = rand.Next(0, 15);
                int[] arr       = new int[arraysize];
                Console.WriteLine(String.Format("****{0} Test{1}", select.GetName(), i + 1));
                for (int j = 0; j < arraysize; j++)
                {
                    arr[j] = rand.Next(nfrom, nto);
                }
                Console.WriteLine(String.Format("****{0} Array = ", select.GetName()));
                Util.PrintArray(arr);
                // select ith largest
                int ith             = rand.Next(1, arr.Length);
                int ithLargestValue = select.SelectIthMax(arr, ith);
                arr = sort.Sort(arr);

                Console.WriteLine(String.Format("****{0} Array Sorted", select.GetName()));
                Util.PrintArray(arr);
                Console.WriteLine(String.Format("****{0} i = {1}, ith = {2}, {2} =? {3}", select.GetName(), ith, arr[ith - 1], ithLargestValue));
                if (arr[ith - 1] != ithLargestValue)
                {
                    Console.WriteLine(String.Format("****{0} Test{1} Faild", select.GetName(), i + 1));
                    return;
                }
                Console.WriteLine(String.Format("****{0} Test{1} Passed!", select.GetName(), i + 1));
            }
        }