public void OnSortingFinished(object source, SorterEventArgs e) { threadCounter++; Printer sortedArrayPrinter = new Printer(); // create printer instance for printing sorted array if (menu.typeOfSorter < sortersList.Count + 1 || threadCounter == sortersList.Count) { sortedArrayPrinter.PrintingFinished += OnPrintingFinished; //subscrubing for printing finished event threadCounter = 0; } lock (locker) { sortedArrayPrinter.Print(SorterUtils.ConvertArrayTo2D(e.array, e.amountOfRaws, e.amountOfColumns), source, e.sortingTime); } }
public event SortingFinishedEventHandler SortingFinished; // event which will be rised after sorting will finish public override void Sort(bool isDescending, int[,] arrayforSorting) { // Getting i and j values from arrived 2D array for future back conversion from 1D to 2D array amountOfRaws = arrayforSorting.GetLength(0); amountOfColumns = arrayforSorting.GetLength(1); array = SorterUtils.ConvertArrayTo1D(arrayforSorting); //converting 2d array to 1d sortingStopwatch.Start(); //starting Stopwatch //Sorting... 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); } } } } sortingStopwatch.Stop(); //Stoppping stopwatch // checking if we need to invert array if (isDescending) { array = SorterUtils.ChangeDirection(array); // inverting array } TimeSpan sortingTime = sortingStopwatch.Elapsed; OnSortingFinished(array, amountOfRaws, amountOfColumns, sortingTime); //rising event }
public event SortingFinishedEventHandler SortingFinished; // event which will be rised after sorting will finish public override void Sort(bool isDescending, int[,] arrayforSorting) { // Getting i and j values from arrived 2D array for future back conversion from 1D to 2D array amountOfRaws = arrayforSorting.GetLength(0); amountOfColumns = arrayforSorting.GetLength(1); array = SorterUtils.ConvertArrayTo1D(arrayforSorting); //converting 2d array to 1d sortingStopwatch.Start(); //starting stopwatch selectSort(array); // Sorting sortingStopwatch.Stop(); //stopping stopwatch // checking if we need to invert array if (isDescending) { array = SorterUtils.ChangeDirection(array); // inverting array } TimeSpan sortingTime = sortingStopwatch.Elapsed; OnSortingFinished(array, amountOfRaws, amountOfColumns, sortingTime); //rising event }
public event SortingFinishedEventHandler SortingFinished; // event which will be rised after sorting will finish public override void Sort(bool isDescending, int[,] arrayforSorting) { bool isSwapped; // Getting i and j values from arrived 2D array for future back conversion from 1D to 2D array amountOfRaws = arrayforSorting.GetLength(0); amountOfColumns = arrayforSorting.GetLength(1); array = SorterUtils.ConvertArrayTo1D(arrayforSorting); //converting 2d array to 1d sortingStopwatch.Start(); //starting stopwatch //Sorting... 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 until there was no swap sortingStopwatch.Stop(); //stopping stopwatch // checking if we need to invert array if (isDescending) { array = SorterUtils.ChangeDirection(array); // inverting array } sortingTime = sortingStopwatch.Elapsed; OnSortingFinished(array, amountOfRaws, amountOfColumns, sortingTime); //rising event }
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'); }
public Sorter(int[,] arrayforSorting) { array = SorterUtils.ConvertArrayTo1D(arrayforSorting); //converting 2d array to 1d in constuctor }
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(); } } }