コード例 #1
0
ファイル: Class1.cs プロジェクト: slavikcx/CSharpBasic
 public static void OnSortingFinished(object source, BubbleSorterEventArgs e)
 {
     Printer.Print(ConvertArrayTo2D(e.array, e.amountOfRaws, e.amountOfColumns));
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: slavikcx/CSharpBasic
        static void Main(string[] args)
        {
            int amountOfRows, amountOfColumns, sorterMenuIterator = 0;

            int[,] array2d;

            int  sortingType, sortingDirection;
            char choice = ' ';

            bool parsingResult;
            bool isDescending = false;

            int allSortersMenuNumber = 0;

            object locker = new object();

            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 amountOfRows);

                    Console.Clear();
                } while (parsingResult != true || amountOfRows == 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(amountOfRows, amountOfColumns);

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


                ArrayList sortersList = new ArrayList(); //change to List ?

                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) //change s
                    {
                        sorterMenuIterator++;
                        Console.WriteLine("{0} - {1}.", sorterMenuIterator, s);
                    }

                    // Adding Last menu item for use all sorters

                    allSortersMenuNumber = sorterMenuIterator + 1;
                    Console.WriteLine("{0} - All sorters", allSortersMenuNumber);


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

                    sorterMenuIterator = 0;

                    Console.Clear();
                } while (parsingResult != true || sortingType > allSortersMenuNumber); // allSortersMenuNumber - lust number in menu


                //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;
                    }
                    else
                    {
                        isDescending = false;
                    }

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


                switch (sortingType) //change with arraylist index
                {
                case 1:
                    Console.WriteLine("Sorted array with {0}", sortersList[0]);
                    Console.WriteLine("");
                    Printer.Print(SorterUtils.ConvertArrayTo2D(bubleSort.Sort(isDescending), amountOfRows, amountOfColumns));
                    break;

                case 2:
                    Console.WriteLine("Sorted array with {0}", sortersList[1]);
                    Console.WriteLine("");
                    Printer.Print(SorterUtils.ConvertArrayTo2D(insertionSort.Sort(isDescending), amountOfRows, amountOfColumns));
                    break;

                case 3:
                    Console.WriteLine("Sorted array with {0}", sortersList[2]);
                    Console.WriteLine("");
                    Printer.Print(SorterUtils.ConvertArrayTo2D(quickSort.Sort(isDescending), amountOfRows, amountOfColumns));
                    break;

                case 4:
                    Console.WriteLine("Sorted array with {0}", sortersList[3]);
                    Console.WriteLine("");
                    Printer.Print(SorterUtils.ConvertArrayTo2D(selectionSort.Sort(isDescending), amountOfRows, amountOfColumns));
                    break;
                }

                //sorting using all sorters
                if (sortingType == allSortersMenuNumber)
                {
                    foreach (ISorter s in sortersList) //change s
                    {
                        int[] value = null;            // creating empty value for returning from thread

                        Stopwatch sortingStopwatch = new Stopwatch();

                        Thread sortThread = new Thread(
                            () => // creating anonymus method which will be run in thread
                        {
                            sortingStopwatch.Start();

                            value = s.Sort(isDescending); // sorting the array inside anonymus method

                            sortingStopwatch.Stop();

                            lock (locker)
                            {
                                Console.WriteLine("");
                                Console.WriteLine("Sorted with {0}", s);
                                Console.WriteLine("");
                                Printer.Print(SorterUtils.ConvertArrayTo2D(value, amountOfRows, amountOfColumns));
                                Console.WriteLine("Sorting time: {0}", sortingStopwatch.Elapsed);
                                Console.WriteLine("");
                            }
                        });

                        sortThread.Start();
                    }
                    //
                    // here we need somehow know when all threads are finished
                }
            } while (choice != 'e');
        }
コード例 #3
0
        public void Start() // rename to init
        {
            int[,] array2d;

            Printer generatedArrayPrinter = new Printer();

            // creating instatnce of each sorter and add to list
            sortersList = new List <ISorter>
            {
                new BubbleSorter(),
                new InsertionSorter(),
                new QuickSorter(),
                new SelectionSorter()
            };

            //subscribing for event from each sorter / /cretae in list

            foreach (ISorter sorter in sortersList)
            {
                sorter.SortingFinished += OnSortingFinished;
            }

            menu.GenerateMenu(sortersList);

            if (menu.isArrayNeedToGenerate == true)
            {
                //Generating and printing generated array
                array2d = SorterUtils.Generate2DArray(menu.amountOfRows, menu.amountOfColumns);
                Console.WriteLine("New {0}X{1} array created and filled with random values \n", menu.amountOfRows, menu.amountOfColumns);
                generatedArrayPrinter.Print(array2d);
            }
            else
            {
                Console.WriteLine("Got array from DataBase");

                //Data Source = NICESRV - 3189\SQLEXPRESS; Initial Catalog = TestData; Integrated Security = True

                SQLDBSource dbSource = new SQLDBSource();
                array2d = dbSource.GetArray(@"NICESRV-3189\SQLEXPRESS", "TestData");
                //array2d = dbSource.GetArray(@"SLAVA-HP\SQLEXPRESS", "ArraysDB");

                generatedArrayPrinter.Print(array2d);
                //array2d = SorterUtils.Generate2DArray(menu.amountOfRows, menu.amountOfColumns);   //Getting array from DB
            }


            if (menu.typeOfSorter <= sortersList.Count)
            {
                // Starting thread and running selected sorter
                Thread sortThread = new Thread( // cretae method
                    () =>                       // creating anonymus method which will be run in thread
                {
                    sortersList[menu.typeOfSorter - 1].Sort(menu.isDescending, array2d);
                });

                sortThread.Start();
            }
            else
            {
                foreach (Sorter sorter in sortersList) //change s
                {
                    Thread sortThread = new Thread(
                        () =>                                    // creating anonymus method which will be run in thread
                    {
                        sorter.Sort(menu.isDescending, array2d); // sorting the array inside anonymus method
                    });

                    sortThread.Start();
                }
            }
        }