Ejemplo n.º 1
0
        public override int[] Sort(bool isDescending)
        {
            for (int j = 0; j < array.Length - 1; j++)
            {
                if (array[j + 1].CompareTo(array[j]) < 0)
                {
                    Swap(j, j + 1);

                    for (int i = j; i > 0; i--)
                    {
                        if (array[i].CompareTo(array[i - 1]) < 0)
                        {
                            Swap(i, i - 1);
                        }
                    }
                }
            }
            // checking if we need to invert array
            if (isDescending)
            {
                array = SorterUtils.ChangeDirection(array); // inverting array
            }

            return(array);
        }
Ejemplo n.º 2
0
        //Bubble sorting method
        public override int [] Sort(bool isDescending)
        {
            bool isSwapped;

            do
            {
                isSwapped = false;
                for (int j = 0; j < array.Length - 1; j++)
                {
                    if (array[j].CompareTo(array[j + 1]) > 0) //comparing values and if next value less than previous  - swapping values
                    {
                        Swap(j, j + 1);
                        isSwapped = true;
                    }
                }
            } while (isSwapped); // repit cycle untill there was no swap

            // checking if we need to invert array
            if (isDescending)
            {
                array = SorterUtils.ChangeDirection(array);  // inverting array
            }

            return(array);
        }
Ejemplo n.º 3
0
        public override int[] Sort(bool isDescending)
        {
            Quicksort(array);

            // checking if we need to invert array
            if (isDescending)
            {
                array = SorterUtils.ChangeDirection(array); // inverting array
            }

            return(array);
        }
Ejemplo n.º 4
0
 public Sorter(int[,] arrayforSorting)
 {
     //array2d = arrayforSorting;
     array = SorterUtils.ConvertArrayTo1D(arrayforSorting); //converting 2d array to 1d in constuctor
     //array2d = arrayforSorting;
 }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            int amountOfRaws, amountOfColumns, amountOfSorters, sorterMenuIterator = 0;

            int[,] array2d;

            int  sortingType, sortingDirection, k = 0;
            char choice = ' ';

            bool parsingResult;
            bool isDescending = false;

            Random rnd = new Random();

            do
            {
                do
                {
                    Console.WriteLine("Please make your choise.");
                    Console.WriteLine("c - create new array");
                    Console.WriteLine("e - exit");

                    choice = (char)Console.Read();


                    Console.Clear();
                } while (choice != 'c' && choice != 'e');


                // if user enter e - breack the loop and exit from program
                if (choice == 'e')
                {
                    break;
                }


                // asking user how much raws should be in 2D array
                do
                {
                    Console.Write("Please enter amount of rows in 2D array - ");

                    parsingResult = Int32.TryParse(Console.ReadLine(), out amountOfRaws);

                    Console.Clear();
                } while (parsingResult != true || amountOfRaws == 0);


                //asking user how much raws should be in 2D array
                do
                {
                    Console.Write("Please enter amount of columns in 2D array - ");

                    parsingResult = Int32.TryParse(Console.ReadLine(), out amountOfColumns);

                    Console.Clear();
                } while (parsingResult != true || amountOfColumns == 0);


                // generating filled 2D array
                array2d = Generate2DArray(amountOfRaws, amountOfColumns);

                Console.WriteLine("New {0}X{1} array created and filled with random values", amountOfRaws, amountOfColumns);
                Printer.Print(array2d);
                Console.WriteLine("");


                ArrayList sortersList = new ArrayList();

                ISorter bubleSort     = new BubbleSorter(array2d);
                ISorter insertionSort = new InsertionSorter(array2d);
                ISorter quickSort     = new QuickSorter(array2d);
                ISorter selectionSort = new SelectionSorter(array2d);

                sortersList.Add(bubleSort);
                sortersList.Add(insertionSort);
                sortersList.Add(quickSort);
                sortersList.Add(selectionSort);


                //defining sorting method
                do
                {
                    Console.WriteLine("Choose sorter type: ");

                    // Generating dynamic menu
                    foreach (ISorter s in sortersList)
                    {
                        sorterMenuIterator++;
                        Console.WriteLine("{0} - {1}.", sorterMenuIterator, s);
                    }

                    parsingResult      = Int32.TryParse(Console.ReadLine(), out sortingType);
                    amountOfSorters    = sorterMenuIterator;
                    sorterMenuIterator = 0;
                    Console.Clear();
                } while (parsingResult != true || sortingType > amountOfSorters);



                //definig array direction
                do
                {
                    Console.WriteLine("");
                    Console.WriteLine("Choose sorting direction: ");
                    Console.WriteLine("1 - Ascending");
                    Console.WriteLine("2 - Descending");

                    parsingResult = Int32.TryParse(Console.ReadLine(), out sortingDirection);

                    if (sortingDirection == 2)
                    {
                        isDescending = true;
                    }

                    Console.Clear();
                } while (parsingResult != true || sortingDirection > 2);



                switch (sortingType)
                {
                case 1:
                    Console.WriteLine("Sorted array:");
                    Printer.Print(SorterUtils.ConvertArrayTo2D(bubleSort.Sort(isDescending), amountOfRaws, amountOfColumns));
                    break;

                case 2:

                    Printer.Print(SorterUtils.ConvertArrayTo2D(insertionSort.Sort(isDescending), amountOfRaws, amountOfColumns));
                    break;

                case 3:
                    Printer.Print(SorterUtils.ConvertArrayTo2D(quickSort.Sort(isDescending), amountOfRaws, amountOfColumns));
                    break;

                case 4:
                    Printer.Print(SorterUtils.ConvertArrayTo2D(selectionSort.Sort(isDescending), amountOfRaws, amountOfColumns));
                    break;
                }
            } while (choice != 'e');
        }