Ejemplo n.º 1
0
            public static double[] Sort(double[] Unsorted, int left, int right)
            {
                //define the intial variables
                int    i     = left;
                int    j     = right;
                double pivot = Unsorted[(left + right) / 2];

                while (i <= j)
                {
                    while (Unsorted[i].CompareTo(pivot) < 0)
                    {
                        i++;
                        QuickCount++;
                    }

                    while (Unsorted[j].CompareTo(pivot) > 0)
                    {
                        j--;
                        QuickCount++;
                    }

                    if (i <= j)
                    {
                        //swap values
                        double tmp = Unsorted[i];
                        Unsorted[i] = Unsorted[j];
                        Unsorted[j] = tmp;
                        QuickCount++;
                        i++;
                        j--;
                    }
                }

                //recursive calls
                if (left < j)
                {
                    QuickSort.Sort(Unsorted, left, j);
                }
                if (i < right)
                {
                    QuickSort.Sort(Unsorted, i, right);
                }
                //return sorted array
                return(Unsorted);
            }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //Get The choice from other method
            string Data = DataSelection();

            //load array via other method
            double[] Unsorted = LoadData(Data);
            //create the array for the sorted array for later
            double[] Sorted = new double[Unsorted.Length];
            //get the values needed from the other methods
            int orient     = Orientation();
            int SortChoice = SortSelection();

            //call the correct sorting algorithm based on search
            if (SortChoice == 0)
            {
                Sorted = BubbleSort.Sort(Unsorted, Unsorted.Length);
            }
            else if (SortChoice == 1)
            {
                Sorted = InsertionSort.Sort(Unsorted, Unsorted.Length);
            }
            else if (SortChoice == 2)
            {
                Sorted = QuickSort.Sort(Unsorted, 0, Unsorted.Length - 1);
            }
            else if (SortChoice == 3)
            {
                Sorted = HeapSort.Sort(Unsorted, Unsorted.Length);
            }
            else
            {
                //exit if they chose to
                System.Environment.Exit(1);
            }
            //Print array either accending or deccending based on choice
            if (orient == 1)
            {
                PrintArrayReversed(Sorted);
            }
            else
            {
                PrintArray(Sorted);
            }
            //switch statement to print correct statement
            switch (SortChoice)
            {
            case 0:
                Console.WriteLine("The number of steps for the bubble sort was {0}", BubbleSort.BubbleCount);
                break;

            case 1:
                Console.WriteLine("The number of steps for the insertion sort was {0}", InsertionSort.InsertionCount);
                break;

            case 2:
                Console.WriteLine("The number of steps for the quick sort was {0}", QuickSort.QuickCount);
                break;

            case 3:
                Console.WriteLine("The number of steps for the heap sort was {0}", HeapSort.HeapCount);
                break;
            }
            //get search algorithm choice from other method
            int    SearchChoice = SearchSelection();
            double value        = SearchItem();

            //run everything for linear search
            if (SearchChoice == 0)
            {
                List <int> Locations = LinearSearch.Search(Sorted, value);
                //if the requested value isnt found
                if (LinearSearch.Linearfound == false)
                {
                    Console.WriteLine("The number {0} was not found.", value);
                    Console.WriteLine("It took {0} steps to confirm the number was not present.", LinearSearch.LinearCount);
                    Console.WriteLine("The closest number was:");
                    Console.WriteLine("{0} at position {1}", LinearSearch.LinearNearest, Locations[0]);
                }
                //if requested value is found
                else
                {
                    Console.WriteLine("The number {0} was found at these positions:", value);

                    //print all locations value was found at
                    foreach (int i in Locations)
                    {
                        Console.Write("{0} ", i);
                    }
                    Console.WriteLine();
                    Console.WriteLine("It took {0} steps to find the number.", LinearSearch.LinearCount);
                }
            }
            //run everything for binary search
            if (SearchChoice == 1)
            {
                List <int> Locations = BinarySearch.Search(Sorted, value);
                //if the requested value isnt found
                if (BinarySearch.Binaryfound == false)
                {
                    Console.WriteLine("The number {0} was not found.", value);
                    Console.WriteLine("It took {0} steps to confirm the number was not present.", BinarySearch.BinaryCount);
                    Console.WriteLine("The closest number was:");
                    Console.WriteLine("{0} at position {1}", BinarySearch.BinaryNearest, Locations[0]);
                }
                //if requested value is found
                else
                {
                    Console.WriteLine("The number {0} was found at these positions:", value);
                    //print all locations value was found at
                    foreach (int i in Locations)
                    {
                        Console.Write("{0} ", i);
                    }
                    Console.WriteLine();
                    Console.WriteLine("It took {0} steps to find the number.", BinarySearch.BinaryCount);
                }
            }
            else
            {
                System.Environment.Exit(1);
            }
        }