Esempio n. 1
0
            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();
            }
Esempio n. 2
0
 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();
     }
 }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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));
        }
Esempio n. 5
0
 protected virtual void OnSortingFinished(EventArgs e)
 {
     SortingFinished?.Invoke(this, e);
 }