• C# 基础排序与查找算法


    排序算法:

        class Sort
        {
            static void swap<T>(ref T a, ref T b)
            {
                T tmp = a;
                a = b;
                b = tmp;
            }
    
            #region 冒泡排序
            public static void Bubble(ref int[] arr)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                    for (int j = i + 1; j < arr.Length; j++)
                        if (arr[i] > arr[j])
                            swap(ref arr[i], ref arr[j]);
            }
            //其它数据类型同上
            public static void Bubble(ref string[] arr)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                    for (int j = i + 1; j < arr.Length; j++)
                        if (arr[i].CompareTo(arr[j]) > 0)
                            swap(ref arr[i], ref arr[j]);
            }
            #endregion
    
            #region 选择排序
            public static void Select(ref int[] arr)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    int min = i;
                    for (int j = i + 1; j < arr.Length; j++)
                        if (arr[j] < arr[min])
                            min = j;
                    swap(ref arr[i], ref arr[min]);
                }
            }
            //其它数据类型同上
            #endregion
    
            #region 插入排序
            public static void Insert(ref int[] arr)
            {
                for (int i = 1; i < arr.Length; i++)
                {
                    int tmp = arr[i];
                    int j = i;
                    while (j > 0 && arr[j - 1] >= tmp)
                    {
                        arr[j] = arr[j - 1];
                        j--;
                    }
                    arr[j] = tmp;
                }
            }
            //其它数据类型同上
            #endregion
        }

    查找算法

        class Search
        {
            static void swap<T>(ref T a, ref T b)
            {
                T tmp = a;
                a = b;
                b = tmp;
            }
    
            #region 顺序查找
            public static int SeqIndex(int[] arr, int val)
            {
                for (int i = 0; i < arr.Length; i++)
                    if (arr[i] == val)
                        return i;
                return -1;
            }
            public static int FindMin(int[] arr)
            {
                int min = arr[0];
                for (int i = 1; i < arr.Length; i++)
                    if (arr[i] < min)
                        min = arr[i];
                return min;
            }
            public static int FindMax(int[] arr)
            {
                int max = arr[0];
                for (int i = 1; i < arr.Length; i++)
                    if (arr[i] > max)
                        max = arr[i];
                return max;
            }
    
            //自组织数据加快顺序查找速度,二八原则,常用的前移
            public static int CustSeqIndex(ref int[] arr, int val)
            {
                for (int i = 0; i < arr.Length; i++)
                    if (arr[i] == val)
                    {
                        if (i > arr.Length * 0.2)
                        {
                            swap(ref arr[i], ref arr[i - 1]);
                            return i - 1;
                        }
                        else return i;
                    }
                return -1;
            }
            #endregion
    
            #region 二叉查找 须对有序数组
            public static int BinaryFind(int[] arr, int val)
            {
                int min = 0, max = arr.Length - 1;
                while (min <= max)
                {
                    int mid = (min + max) / 2;
                    if (arr[mid] < val)
                        min = mid + 1;
                    else if (arr[mid] > val)
                        max = mid - 1;
                    else return mid;
                }
                return -1;
            }
            //用递归法重写上述功能,效率没上面循环方法高
            public static int RBinaryFind(int[] arr, int val, int min, int max)
            {
                if (min > max)
                    return -1;
                else
                {
                    int mid = (min + max) / 2;
                    if (arr[mid] == val)
                        return mid;
                    else
                    {
                        if (arr[mid] < val)
                            min = mid + 1;
                        else max = mid - 1;
                        return RBinaryFind(arr, val, min, max);
                    }
                }
            }
            #endregion
        }
  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/lged/p/5889814.html
Copyright © 2020-2023  润新知