首页 > 代码库 > 排序算法

排序算法

 class SortArrange    {        public struct SqlList        {           public int[] data;           public int length;        }        private static void Swap(ref SqlList L, int i, int j)        {            int temp = L.data[i];            L.data[i] = L.data[j];            L.data[j] = temp;        }        /// <summary>        /// 最基础的排序算法,效率最差,不算标准的冒泡排序算法,不满足“两两相邻要素比较”,只是让每一个关键字比较交换得出;        /// </summary>        /// <param name="L"></param>        public static int BubbleSortBase(SqlList L)        {            int count = 0;            for(int i=0;i<L.length;i++)            {                for (int j = i + 1; j < L.length; j++)                {                    if (L.data[i] > L.data[j])                    {                        Swap(ref L, i, j);                        count++;                    }                }            }            return count;        }        /// <summary>        /// 标准的冒泡排序,实现两两比较互换,效率高于基础排序算法        /// </summary>        /// <param name="L">排序数组</param>        public static int BubbleSortStandard(SqlList L)        {            int count = 0;            for (int i = 0; i < L.length; i++)            {                for (int j = L.length - 2; j >= i; j--)                {                    if (L.data[j] > L.data[j + 1])                    {                        Swap(ref L, j, j + 1);                        count++;                    }                }            }            return count;        }        /// <summary>        /// 用flag标志判断在已经有序的情况下进行不需要的判断        /// </summary>        /// <param name="L">排序数组</param>        public static int BubbleSortImprove(SqlList L)        {            int count = 0;            bool flag = true;            for (int i = 0; i < L.length&&flag; i++)            {                flag = false;                for (int j = L.length - 2; j >= i; j--)                {                    if (L.data[j] > L.data[j + 1])                    {                        Swap(ref L, j, j + 1);                        flag = true;                        count++;                    }               }            }            return count;        }        /// <summary>        /// 简单选择排序,算法类似基础排序算法,减少了交换次数,尽管没提高比较次数,但是减少了多余的交换        /// 性能上略优于冒泡排序,时间复杂度为O(n2)        /// </summary>        /// <param name="L"></param>        public static int SelectSort(SqlList L)        {            int i, j, min,count=0;            for (i = 0; i < L.length; i++)            {                min = i;                for (j = i + 1; j < L.length; j++)                {                    if (L.data[min] > L.data[j])                    {                        min = j;                    }                }                if (min != i)                {                    Swap(ref L, i, min);                    count++;                }            }            return count;        }        /// <summary>        /// 直接插入排序,默认第一个数是排序好的,只是插入有序数列的左右边选择问题,性能优于冒泡排序和选择排序        /// 需要比较次数:(n+2)(n-1)/2,移动的次数:(n+4)(n-1)/2,时间复杂度为O(n2)        /// </summary>        /// <param name="L"></param>        public static int StraightInsertSort(SqlList L)        {            int i, j,temp,count=0;            for (i = 1; i < L.length; i++)            {                if (L.data[i] < L.data[i - 1])                {                    temp = L.data[i];                    for (j = i - 1; j >= 0 && L.data[j] > temp; j--)                    {                        L.data[j + 1] = L.data[j];                        count++;                    }                    L.data[j + 1] = temp;                }            }            return count;        }    }

  

排序算法