Esempio n. 1
0
        public CH0503()
        {
            int[]    arrs     = new int[] { 1, 4, 5, 21, 9, 3, 2 };
            SortThan sortThan = new SortThan(Compare2);

            DelegateSort.BubbleSort(arrs, sortThan);
            foreach (var item in arrs)
            {
                Console.WriteLine($"{item}");
            }
        }
Esempio n. 2
0
        public static void BubbleSort(int[] arr, SortThan sortThan)
        {
            int i, j, t;

            if (arr == null)
            {
                return;
            }
            for (i = arr.Length - 1; i > 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (sortThan(arr[j - 1], arr[j]))
                    {
                        t          = arr[j - 1];
                        arr[j - 1] = arr[j];
                        arr[j]     = t;
                    }
                }
            }
        }
        public static void BubbleSort(int[] items, SortThan sortThan)
        {
            int i, j, temp;

            if (items == null)
            {
                return;
            }
            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    //if (items[j - 1] > items[j])
                    if (sortThan(items[j - 1], items[j]))
                    {
                        temp         = items[j - 1];
                        items[j - 1] = items[j];
                        items[j]     = temp;
                    }
                }
            }
        }