zoukankan      html  css  js  c++  java
  • 洗牌算法

    最近要做一个程序随机出数据的程序,最后找到了一个性能不错的算法 洗牌算法

    Random ram = new Random()

     //随机交换
                int currentIndex;
                Product tempValue;
                for (int i = 0; i < listtemp.Count; i++)
                {
                    currentIndex = ram.Next(0, listtemp.Count - i);
                    tempValue = listtemp[currentIndex];
                    listtemp[currentIndex] = listtemp[listtemp.Count - 1 - i];

    /// <summary>
            /// 洗牌算法
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="listtemp"></param>
            public static List<T> Reshuffle<T>(List<T> listtemp)
            {
                //随机交换
                Random ram = new Random();
                int currentIndex;
                T tempValue;
                for (int i = 0; i < listtemp.Count; i++)
                {
                    currentIndex = ram.Next(0, listtemp.Count - i);
                    tempValue = listtemp[currentIndex];
                    listtemp[currentIndex] = listtemp[listtemp.Count - 1 - i];
                    listtemp[listtemp.Count - 1 - i] = tempValue;
                }

                return listtemp;

            }

     


  • 相关阅读:
    DS博客作业06--图
    DS博客作业05--树
    DS博客作业03--栈和队列
    DS博客作业02-线性表
    DS博客作业01--日期抽象数据类型设计与实现
    C语言博客作业06---结构体&文件
    C语言博客作业05---指针
    C语言博客作业04--数组
    DS博客作业08--课程总结
    DS博客作业07--查找
  • 原文地址:https://www.cnblogs.com/liyonghui/p/2763821.html
Copyright © 2011-2022 走看看