Exemplo n.º 1
0
        public void SortArray <T>(T[] array, ComparisonDel <T> comparison, Action <string> callback) where T : IComparable <T>
        {
            T temp = default(T);

            string arrayType = array.ToString();

            if (comparison != null)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    for (int j = i + 1; j < array.Length; j++)
                    {
                        if (comparison(array[i], array[j]))
                        {
                            temp     = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                        Console.WriteLine($" - - - В массиве {arrayType} произошло сравнение {array[i]} и {array[j]}");

                        Thread.Sleep(50);
                    }
                }
            }

            callback?.Invoke(arrayType);
        }
Exemplo n.º 2
0
        //Variant 2: Callback signals about the end of the sorting
        private static void ShowSortWithCallback(ComparisonDel <int> compInt, ComparisonDel <double> compDouble, ComparisonDel <string> compString)
        {
            SortingUnit2 sortUnit = new SortingUnit2();

            sortUnit.SortInNewThread(arrayInt, compInt);
            sortUnit.SortInNewThread(arrayDouble, compDouble);
            sortUnit.SortInNewThread(arrayString, compString);
        }
Exemplo n.º 3
0
        public void SortInNewThread <T>(T[] array, ComparisonDel <T> comparison) where T : IComparable <T>
        {
            Thread newThread = new Thread(
                () => SortArray(array, comparison, SortEndHandler));

            Console.WriteLine($"Запускается поток для сортировки массива {array.ToString()}, состоящего из {array.Length} элементов:");

            newThread.Start();
        }
Exemplo n.º 4
0
        static void SortingUnitDemo()
        {
            ComparisonDel <int>    comparisonInt    = Comparison;
            ComparisonDel <double> comparisonDouble = Comparison;
            ComparisonDel <string> comparisonString = Comparison;

            Console.WriteLine("{0} * * * ОБ ОКОНЧАНИИ СОРТИРОВКИ СИГНАЛИЗИРУЕТ СОБЫТИЕ * * *{0}", Environment.NewLine);

            ShowSortWithEvent(comparisonInt, comparisonDouble, comparisonString);

            Console.WriteLine("{0} * * * УЗНАЕМ ОБ ОКОНЧАНИИ СОРТИРОВКИ ЧЕРЕЗ CALLBACK * * *{0}", Environment.NewLine);

            ShowSortWithCallback(comparisonInt, comparisonDouble, comparisonString);
        }
Exemplo n.º 5
0
        static void CustomSortDemo()
        {
            string[] array = new string[] { "Мадрид", "Осло", "Сеул", "Москва", "Афины", "Рим", "Лиссабон", "Рига", "Шри-Джаяварденепура-Котте", "Баку" };

            Console.WriteLine("{0}Исходный массив:{0}", Environment.NewLine);
            Show(array);

            ComparisonDel <string> comparisonDelegate = ComparisonStrings;

            SortArray(array, comparisonDelegate);

            Console.WriteLine("{0}{0}Отсортированный массив:{0}", Environment.NewLine);
            Show(array);

            ToNext();
        }
Exemplo n.º 6
0
        static void CustomSort()
        {
            int[] array = new int[] { 10, 2, 5, 4, 3, 4, 8, 9, 1, 99, 25, 40, 3 };

            Console.WriteLine("Исходный массив:{0}", Environment.NewLine);
            Show(array);

            ComparisonDel <int> comparisonDelegate = Comparison;

            SortArray(array, comparisonDelegate);

            Console.WriteLine("{0}{0}Отсортированный массив:{0}", Environment.NewLine);
            Show(array);

            ToNext();
        }
Exemplo n.º 7
0
        //Variant 1: The event signals about the end of the sorting
        private static void ShowSortWithEvent(ComparisonDel <int> compInt, ComparisonDel <double> compDouble, ComparisonDel <string> compString)
        {
            SortingUnit sortUnit = new SortingUnit();
            Handler     handler  = new Handler();

            sortUnit.OnSorted += handler.Message;


            sortUnit.SortInNewThread(arrayInt, compInt);
            sortUnit.SortInNewThread(arrayDouble, compDouble);
            sortUnit.SortInNewThread(arrayString, compString);

            sortUnit.OnSorted -= handler.Message;

            sortUnit.SortInNewThread(arrayString, compString);

            ToNext();
        }
Exemplo n.º 8
0
        static void SortArray <T>(T[] array, ComparisonDel <T> comparison) where T : IComparable <T>
        {
            T temp = default(T);

            if (comparison != null)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    for (int j = i + 1; j < array.Length; j++)
                    {
                        if (comparison(array[i], array[j]))
                        {
                            temp     = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                    }
                }
            }
        }