桶排序——代码

之前我们已经学习了很多的排序算法,其中包括选择,交换,插入,归并等等常用的排序,今天我们来讲一讲桶排序,不过讲之前我们首先来回顾一下以前学过的排序。

排序算法图
除了这些排序,还有基数排序,桶排序,计数排序。今天我们就来分享下桶排序。
见代码:

namespace 桶排序重构__Aran
{
    /*
     * 功能
     * 桶排序
     * 把区间[0,1){大桶}划分成n个相同大小的子区间(桶)
     * 将n个输入的数分布到各个桶中去(利用函数关系)
     * 对桶中的数进行排序
     * 按照次序把各个桶中的元素输出
     * Aran
     * 2018-09-15
     * */
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 7, 26, 53, 22, 32, 28, 38, 23, 24, 54, 27, 78, 33 };
            Program bus = new Program();
            Console.WriteLine("桶排序:");
            bus.MyBucketsort(arr);

        }

        //构造函数关系
        /// <summary>
        /// 桶排序---映射关系F(x)=(int)Math.Sqrt(x)平方根
        /// </summary>
        /// <param name="arr">数组</param>
        public void MyBucketsort(int[] arr)
        {
            //判断数组长度
            int lenth = arr.Length;
            if (lenth<2)
            {
                return;
            }

            #region//设置映射关系为,并求出函数映射值的最大最小值——根据最大值确定大桶的个数
            int[] reciprocal = new int[lenth];//定义数组,长度为已有数组长度
            int min = arr[0], max = arr[0];
            for (int i = 0; i < lenth; i++)
            {
                reciprocal[i] = (int)Math.Sqrt(arr[i]);//数组用来接收函数关系的结果
                if (min > reciprocal[i])
                {
                    min = reciprocal[i];
                }
                else if (max < reciprocal[i])
                {
                    max = reciprocal[i];
                }
                else { }

            }
            #endregion


            #region//设置大桶的最大序号为max,则大桶的数量为max+1,//(也可以设置桶的数量为max-mix+1),则arr[max-1]可以代表arr[i]
            int tLen = max + 1;
            #endregion

            //每个大桶也是一个数组

            #region//使用交叉数组(二位数组)定义,当后面出现桶的序号的时候才进行实例化,减少内存占用,并方便判断那些桶有数据(懒汉式)
            int[][] temp = new int[tLen][];
            for (int i = 0; i < lenth; i++)
            {
                #region//根据函数映射找到桶的下标,将对应的原数组的值放进桶中
                int n = reciprocal[i];//对应的大桶名(2,5,7,.....)
                if (temp[n]==null)
                {
                    //不知道该位数有多少,设置长度为原数组的长度(不确定传进来的数组多大)
                    temp[n] = new int[lenth];
                   //实现二维数组,每个大桶定义成一个数组
                    //初始化数组值为一定不存 temp[n] = new int[lenth]在的值,把不为空的数组中没用的位置标为-1
                    for (int j = 0; j < lenth; j++)
                    {
                        temp[n][j] = -1;
                    }
                    temp[n][0] = arr[i];
                }
                else//有数据了temp[n]!=null
                {
                    //remp[n]已存在时候的循环temp[n],找到值不是-1的索引,并将arr[i]对应的值放进去
                    int j = 0;
                    while (temp[n][j]!=-1)//这个位置已经有数存入了
                    {
                        j++;
                    }
                    temp[n][j] = arr[i];//把需要排序的数组的数赋值给小桶
                }
                #endregion
            }
            #endregion

            #region//对桶内的元素进行快速排序,并且依次将桶内元素添加到新的数组(根据根号关系可知,序号小的桶内元素一定小于序号大的桶内元素)
            int index = 0;
            QuickSort qs = new QuickSort();
            for (int i = 0; i < tLen; i++)
            {
                if (temp[i] != null)
                {
                    Console.WriteLine("第"+i+"个桶内快排:");
                    temp[i] = qs.MyQuickSort(temp[i], 0, lenth - 1);
                    for (int j = 0; j < lenth; j++)
                    {
                        if (temp[i][j]!=-1)
                        {
                            arr[index++] = temp[i][j];
                        }
                    }
                }
            }
            #endregion


            #region//输出排序结果
            Console.WriteLine("排序结果:");
            foreach (int k in arr)
            {
                Console.WriteLine(k+" ");
            }
            //换行,每一轮为一行
            Console.WriteLine();
            Console.ReadKey();
            #endregion
        }


    }
}

桶排序详解,请点击写篇博客链接(待发布~),大家先理解下代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值