c#泛型算法

using System;

/// <summary>
/// 平均时间复杂度从高到低依次是:
///冒泡排序(o(n2)),选择排序(o(n2)),插入排序(o(n2)),堆排序(o(nlogn)),
/// 
///归并排序(o(nlogn)),快速排序(o(nlogn)), 希尔排序(o(n1.25)),基数排序(o(n))
/// </summary>
public class Program
{

    static void Main(string[] args)
    {
        int[] intNums = { 1, 3, 54, 5, 6, 7, 8, 67, 9 };

        string[] arr = { "w", "s", "t", "a", "9", "e", "6" };

        //  BubbleSort<int>(intNums, (a, b) => { return a < b; });
        //  SelectSort<int>(intNums, (a, b) => { return a < b; });
       // InsertSort<int>(intNums, (a, b) => { return a < b; });


       // InsertSortImprovedWithBinarySearch<int>(intNums, (a, b) => { return a > b; });


       // QuickSortStrict<int>(intNums, (a, b) => { return a > b; });

        ShellSort<int>(intNums, (a, b) => { return a > b; });
        Print<int>(intNums);


        BubbleSort<string>(arr, (a, b) => a.CompareTo(b) > 0);
        Print<string>(arr);
        Console.ReadKey();
    }
    public static void Print<T>(T[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write(arr[i] + " ");
        }
        Console.WriteLine();
    }
    private static void Swap<T>(T[] datas, int j, int i)
    {
        T temp = datas[j];
        datas[j] = datas[i];
        datas[i] = temp;
    }

    /// <summary>
    /// 排序交换条件委托
    /// </summary>
    public delegate bool Compare<T>(T t1, T t2);
    #region     冒泡
    /// <summary>
    /// 冒泡排序,compare为排序交换的条件
    /// 从头开始,每一个元素和它的下一个元素比较,如果它大,就将它与比较的元素交换,否则不动。
    /// 这意味着,大的元素总是在向后慢慢移动直到遇到比它更大的元素。所以每一轮交换完成都能将最大值冒到最后。
    /// </summary>
    public static void BubbleSort<T>(T[] datas, Compare<T> compare)
    {
        for (int i = 0; i < datas.Length; i++)
        {
            for (int j = 0; j < datas.Length - 1 - i; j++)
            {
                if (compare(datas[j], datas[j + 1]))
                {
                    Swap<T>(datas, j, j + 1);
                }
            }
        }
    }
    #endregion

    #region     选择
    /// <summary>
    /// 选择排序:原理:找出参与排序的数组最大值,放到末尾(或找到最小值放到开头)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="datas"></param>
    /// <param name="compare"></param>
    public static void SelectSort<T>(T[] datas, Compare<T> compare)
    {
        for (int i = 0; i < datas.Length - 1; i++)
        {
            int min = i;
            T temp = datas[i];
            for (int j = i + 1; j < datas.Length; j++)
            {
                if (compare(datas[j], temp))
                {
                    min = j;
                    temp = datas[j];
                }
            }
            if (min != i)
                Swap(datas, min, i);
        }
    }


    #endregion
    #region     插入排序
    public static void InsertSort<T>(T[] datas, Compare<T> compare)
    {
        T temp;
        for (int i = 1; i < datas.Length; i++)
        {
            temp = datas[i];
            for (int j = i - 1; j >= 0; j--)
            {
                if (compare(temp, datas[j] ))
                {
                    datas[j + 1] = datas[j];
                    if (j == 0)
                    {
                        datas[0] = temp;
                        break;
                    }
                }
                else
                {
                    datas[j + 1] = temp;
                    break;
                }
            }
        }
    }
    #endregion
    #region   二分查找

    public static void InsertSortImprovedWithBinarySearch<T>(T[]datas, Compare<T> compare)
    {
        T temp;
        int tempIndex;
        for (int i = 1; i < datas.Length; i++)
        {
            temp = datas[i];
            tempIndex = BinarySearchForInsertSort(datas, 0, i, i,compare);
            for (int j = i - 1; j >= tempIndex; j--)
            {
                datas[j + 1] = datas[j];
            }
            datas[tempIndex] = temp;
        }
    }

    public static int BinarySearchForInsertSort<T>(T[] datas, int low, int high, int key,Compare<T>  compare)
    {
        if (low >= datas.Length - 1)
            return datas.Length - 1;
        if (high <= 0)
            return 0;
        int mid = (low + high) / 2;
        if (mid == key) return mid;
        if (compare(datas[key] , datas[mid]))
        {
            if (compare(datas[key] , datas[mid + 1]))
                return mid + 1;
            return BinarySearchForInsertSort(datas, mid + 1, high, key,compare);
        }
        else  
        {
            if (mid - 1 < 0) return 0;
            if (compare(datas[key] , datas[mid - 1]))
                return mid;
            return BinarySearchForInsertSort(datas, low, mid - 1, key, compare);
        }
    }

    #endregion
    #region     快速排序


    /// <summary>
    ///    从数列中挑选一个数作为“哨兵”,使比它小的放在它的左侧,比它大的放在它的右侧。将要排序是数列递归地分割到
    ///        最小数列,每次都让分割出的数列符合“哨兵”的规则,自然就将数列变得有序。
    /// </summary>
    public static void QuickSortStrict<T>(T[] datas, Compare<T> compare)
    {
        QuickSortStrict(datas, 0, datas.Length - 1,compare);
    }

    public static void QuickSortStrict<T>(T[] datas, int low, int high, Compare<T> compare)
    {
        if (low >= high) return;
        T temp = datas[low];
        int i = low + 1, j = high;
        while (true)
        {
            while (compare(datas[j] , temp)) j--;
            while (compare(datas[i] , temp) && i < j) i++;
            if (i >= j) break;
            Swap(datas, i, j);
            i++; j--;
        }
        if (j != low)
            Swap(datas, low, j);
        QuickSortStrict<T>(datas, j + 1, high, compare);
        QuickSortStrict<T>(datas, low, j - 1, compare);
    }
    #endregion
    #region     希尔排序

    /// <summary>
    /// 通过奇妙的步长,插入排序间隔步长的元素,随后逐渐缩短步长至1,实现数列的插入排序。
    /// </summary>
    /// <param name="data"></param>
    public static void ShellSort<T>(T[] datas,Compare<T>  compare)
    {
        T temp;
        for (int gap = datas.Length / 2; gap > 0; gap /= 2)
        {
            for (int i = gap; i < datas.Length; i ++)
            {
                temp = datas[i];
                for (int j = i - gap; j >= 0; j -= gap)
                {
                    if (compare(datas[j] , temp))
                    {
                        datas[j + gap] = datas[j];
                        if (j == 0)
                        {
                            datas[j] = temp;
                            break;
                        }
                    }
                    else
                    {
                        datas[j + gap] = temp;
                        break;
                    }
                }
            }
        }
    }

    #endregion
}

猜你喜欢

转载自blog.csdn.net/z502768095/article/details/81117959