public static void RunSortInNewThread <T>(T[] a, Comparison <T> comp) { ThreadStart thStart = new ThreadStart( () => { Console.WriteLine("New thread start"); BubbleSort(a, comp); SortingFinished?.Invoke("Thread finished"); } ); Thread th = new Thread(thStart); th.Start(); }
public static void SortAsync <T>(T[] arr, Func <T, T, bool> compare) { Thread[] threads = new Thread[4]; for (int i = 0; i < 4; i++) { threads[i] = new Thread(() => { Thread.Sleep(5000); Console.WriteLine($"Starting Thread #{Thread.CurrentThread.ManagedThreadId}.\n Asynchronous array sorting: "); CustomSort <T>(arr, compare); PrintArray <T>(arr); SortingFinished?.Invoke("Sorting Finished!"); }); } foreach (Thread t in threads) { t.Start(); } }
public static T[] Sort(T[] arr, Func <T, T, int> compare) { for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length; j++) { T tmp = arr[i]; if (compare?.Invoke(arr[i], arr[j]) < 0) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } } SortingFinished?.Invoke(arr, EventArgs.Empty); return(arr); }
public void CustomSort(T[] arr, SortingMethod <T> sortingMethod) { bool isSorted = false; T temp; while (!isSorted) { isSorted = true; for (int i = arr.GetLowerBound(0); i < arr.GetUpperBound(0); i++) { if (sortingMethod(arr[i], arr[i + 1])) { temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; isSorted = false; } } } SortingFinished?.Invoke(this, new SortingEventArgs <T>(arr)); }
protected virtual void OnSortingFinished(EventArgs e) { SortingFinished?.Invoke(this, e); }