原文链接:https://www.cnblogs.com/PJQOOO/p/11669493.html
感觉各种算法真实太神奇了
static void Main(string[] args) { int count = 0; M: MyArrayList mal = new MyArrayList(); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start();//开始计时 int[] array = mal.CreateArray(100000,1000000); watch.Stop(); //Console.WriteLine(String.Join(",", array)); Console.WriteLine("数组数量(" + array.Length + ")-------------创建数组耗时:" + watch.ElapsedMilliseconds + "毫秒"); watch = new System.Diagnostics.Stopwatch(); watch.Start(); int[] array1 = (int[])array.Clone(); //冒泡排序 for (int i = 0; i < array1.Length - 1; i++) { for (int j = i + 1; j < array1.Length; j++) { if (array1[i] < array1[j]) { int temp = array1[i]; array1[i] = array1[j]; array1[j] = temp; } } } watch.Stop(); //Console.WriteLine(String.Join(",", array1)); Console.WriteLine("数组数量(" + array1.Length + ")---------------冒泡排序耗时:" + watch.ElapsedMilliseconds + "毫秒"); int[] array2 = (int[])array.Clone(); watch = new System.Diagnostics.Stopwatch(); watch.Start(); //选择排序 for (int i = 0; i < array2.Length - 1; i++) { int maxIndex = i; for (int j = i + 1; j < array2.Length; j++) { if (array2[j] > array2[maxIndex]) { maxIndex = j; } } int temp = array2[i]; array2[i] = array2[maxIndex]; array2[maxIndex] = temp; } watch.Stop(); //Console.WriteLine(String.Join(",", array2)); Console.WriteLine("数组数量(" + array2.Length + ")---------------选择排序耗时:" + watch.ElapsedMilliseconds + "毫秒"); int[] array3 = (int[])array.Clone(); watch = new System.Diagnostics.Stopwatch(); watch.Start(); //插入排序 for (int i = 1; i < array3.Length; i++) { int current = array3[i]; int preIndex = i - 1; while (preIndex >= 0 && current > array3[preIndex]) { array3[preIndex + 1] = array3[preIndex]; preIndex--; } array3[preIndex + 1] = current; } watch.Stop(); //Console.WriteLine(String.Join(",", array3)); Console.WriteLine("数组数量(" + array3.Length + ")---------------插入排序耗时:" + watch.ElapsedMilliseconds + "毫秒"); int[] array4 = (int[])array.Clone(); watch = new System.Diagnostics.Stopwatch(); watch.Start(); //希尔排序 int current1, gap = array4.Length / 2; while (gap > 0) { for (int i = gap; i < array4.Length; i++) { current1 = array4[i]; int preIndex = i - gap; while (preIndex >= 0 && array4[preIndex] > current1) { array4[preIndex + gap] = array4[preIndex]; preIndex -= gap; } array4[preIndex + gap] = current1; } gap /= 2; } watch.Stop(); //Console.WriteLine(String.Join(",", array4)); Console.WriteLine("数组数量(" + array4.Length + ")---------------希尔排序耗时:" + watch.ElapsedMilliseconds + "毫秒"); //循环次数 if (count < 100) { Console.WriteLine("-----------------------------------------------------"); count += 1; goto M; } string word = Console.ReadLine(); if (word == "more") { goto M; } }
/// <summary> /// 创建随机数量的随机数组 /// </summary> /// <param name="count">数组长度</param> /// <param name="number">随机数最大值(最小值默认为1)</param> /// <returns></returns> public int[] CreateArray(int count,int number) { int length = new Random().Next(1, count); int[] array=new int[length]; Random random = new Random(); for (int i = 0; i < length; i++) { int item = random.Next(1, number); array[i] = item; } return array; }
才疏学浅只能看懂到希尔排序
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)