Exemple #1
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()));
        }
Exemple #2
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);
        }
Exemple #3
0
        private static void QuickSortSequence()
        {
            Sorting opt = ShowSortingOption();

            Console.WriteLine("Input space separated integers to sort:");

            int[] toSort = ReadInput();

            if (opt == Sorting.ASC)
            {
                toSort = QuickSort.SortAsc(toSort);
            }
            else
            {
                toSort = QuickSort.SortDesc(toSort);
            }

            Console.WriteLine("Sorted sequence:");
            PrintArray(toSort);
        }
Exemple #4
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();
        }
Exemple #5
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));
            }
        }
Exemple #6
0
        public void QuickSort_canSort()
        {
            var resultArray = new QuickSort().GetOrdered(UnsortedArray);

            Assert.AreEqual(resultArray, SortedArray);
        }
Exemple #7
0
        static void Main(string[] args)
        {
            int[] data = new int[] { 5, 2, 1, 4, 3, 5, 0, 9, 5, 4, 3, 2, 1, 0 };

            Console.WriteLine("Heap Sort");
            int[] sorted1 = new HeapSort(data, data.Length).Sort();
            HeapSort.PrintArray(sorted1, sorted1.Length);

            Console.WriteLine("Quick Sort");
            int[] sorted2 = new QuickSort(data, data.Length).Sort();
            QuickSort.PrintArray(sorted1, sorted1.Length);

            Console.WriteLine("Merge Sort");
            int[] sorted3 = new MergeSort(data, data.Length).Sort();
            MergeSort.PrintArray(sorted1, sorted1.Length);

            Console.WriteLine("Graph - BFS");
            Graph g = new Graph(4);

            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(1, 2);
            g.AddEdge(2, 0);
            g.AddEdge(2, 3);
            g.AddEdge(3, 3);

            g.BFS(2, (i) => Console.Write("{0} ", i));
            Console.WriteLine(string.Empty);

            Console.WriteLine("Graph - DFS");
            g.DFS(2, (i) => Console.Write("{0} ", i));
            Console.WriteLine(string.Empty);

            Graph g1 = new Graph(9);
            List <List <int> > g2 = new List <List <int> >()
            {
                new List <int>()
                {
                    0, 4, 0, 0, 0, 0, 0, 8, 0
                },
                new List <int>()
                {
                    4, 0, 8, 0, 0, 0, 0, 11, 0
                },
                new List <int>()
                {
                    0, 8, 0, 7, 0, 4, 0, 0, 2
                },
                new List <int>()
                {
                    0, 0, 7, 0, 9, 14, 0, 0, 0
                },
                new List <int>()
                {
                    0, 0, 0, 9, 0, 10, 0, 0, 0
                },
                new List <int>()
                {
                    0, 0, 4, 14, 10, 0, 2, 0, 0
                },
                new List <int>()
                {
                    0, 0, 0, 0, 0, 2, 0, 1, 6
                },
                new List <int>()
                {
                    8, 11, 0, 0, 0, 0, 1, 0, 7
                },
                new List <int>()
                {
                    0, 0, 2, 0, 0, 0, 6, 7, 0
                }
            };

            for (int i = 0; i < g2.Count; i++)
            {
                for (int j = 0; j < g2[i].Count; j++)
                {
                    if (g2[i][j] > 0)
                    {
                        g1.AddEdge(i, j, g2[i][j]);
                    }
                }
            }

            g1.PrintDistance(g1.Dijakstra(0), 0);
            Console.WriteLine(string.Empty);

            Console.WriteLine("Binary Search");
            int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            foreach (int i in new int[] { -5, 1, 5, 8, 0, 10, 11 })
            {
                Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearch(arr, i));
            }

            Console.WriteLine("Binary Search Next greatest - 1");
            int[] arr2 = new int[] { 100, 50, 40, 20, 10 };
            foreach (int i in new int[] { 5, 25, 50, 120 })
            {
                Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearchNextGreatest(arr2, i));
            }

            Console.WriteLine("Binary Search Next greatest - 2");
            int[] arr3 = new int[] { 100, 90, 80, 75, 60 };
            foreach (int i in new int[] { 50, 65, 77, 90, 102 })
            {
                Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearchNextGreatest(arr3, i));
            }

            Console.ReadKey();
        }
Exemple #8
0
        private static void QuickSort()
        {
            var sort = new QuickSort();

            sort.Run();
        }
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());

            int[]  A      = new int[n];
            Random random = new Random();

            for (int i = 0; i < n; ++i)
            {
                A[i] = random.Next(0, 100);
                Console.WriteLine(A[i] + " ");
            }


            string a    = Console.ReadLine();
            int    size = a.Length;

            char[] b = new char[size];
            Change(a, b);

            for (int i = 0; i < b.Length; ++i)
            {
                switch (b[i])
                {
                case '1':
                    Console.WriteLine("Insertion Sort ");
                    int[] B = new int[A.Length];
                    for (int k = 0; k < B.Length; ++k)
                    {
                        B[k] = A[k];
                    }
                    InsertionSort.insertionSort(B);
                    InsertionSort.PrintArr(B);
                    break;

                case '2':
                    Console.WriteLine("Merge Sort");
                    int[] C = new int[A.Length];
                    for (int k = 0; k < C.Length; ++k)
                    {
                        C[k] = A[k];
                    }
                    MergeSort.mergeSort(C, 0, C.Length - 1);
                    MergeSort.PrintArr(C);
                    break;

                case '3':
                    Console.WriteLine("Bubble Sort");
                    int[] D = new int[A.Length];
                    for (int k = 0; k < D.Length; ++k)
                    {
                        D[k] = A[k];
                    }
                    BubbleSort.BBSort(D);
                    BubbleSort.PrintArr(D);
                    break;

                case '4':
                    Console.WriteLine("Quick Sort");
                    int[] E = new int[A.Length];
                    for (int k = 0; k < E.Length; ++k)
                    {
                        E[k] = A[k];
                    }
                    QuickSort.quicksort(E, 0, E.Length - 1);
                    QuickSort.PrintArr(E);
                    break;
                }
            }
            Console.ReadKey();
        }