1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
  15. List<List<int>> array = new List<List<int>>();
  16. //定义一个一维list,作为上面二维list的某个元素
  17. List<int> item = new List<int>(new int[] { , , , });
  18. //将上面的一维list作为一个元素放入二维list中
  19. array.Add(item);
  20. //给一维list赋新值
  21. item = new List<int>(new int[] { , , , });
  22. //将上面的一维list作为一个元素放入二维list中
  23. array.Add(item);
  24. //给一维list赋新值
  25. item = new List<int>(new int[] { , , , });
  26. //将上面的一维list作为一个元素放入二维list中
  27. array.Add(item);
  28.  
  29. //下面 取出二维list的某个元素
  30. int m = array[][];//此时的m即为50
  31. //下面 给二维list某位置赋值
  32. array[][] = ;
  33. //验证某位置的值是否改变
  34. m = array[][];
  35. //将二维list的第0行所有元素给某个一维list
  36. item = array[];
  37.  
  38. ////下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
  39. //list排序方法一
  40. //array.Sort( delegate(List<int> p1,List<int> p2)
  41. // {
  42. // return p1[3].CompareTo(p2[3]);//按最后一个元素升序
  43. // }
  44. // );//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
  45. //list排序方法二
  46. array.Sort((List<int> x, List<int> y) => { return x[].CompareTo(y[]); });
  47. //list排序方法三
  48. List<List<int>> array1 = array.OrderBy(o => o[]).ToList();//升序
  49.  
  50. //计时
  51. Stopwatch sw = new Stopwatch();
  52. sw.Start();
  53. Thread.Sleep();
  54. sw.Stop();
  55. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  56. Console.ReadKey();
  57.  
  58. }
  59. }
  60. }

二维list代替二维数组

第一种排序最快

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
  15. List<List<int>> array = new List<List<int>>();
  16. //定义一个一维list,作为上面二维list的某个元素
  17. List<int> item = new List<int>(new int[] { , , , });
  18. //将上面的一维list作为一个元素放入二维list中
  19. array.Add(item);
  20. //给一维list赋新值
  21. item = new List<int>(new int[] { , , , });
  22. //将上面的一维list作为一个元素放入二维list中
  23. array.Add(item);
  24. //给一维list赋新值
  25. item = new List<int>(new int[] { , , , });
  26. //将上面的一维list作为一个元素放入二维list中
  27. array.Add(item);
  28. List<List<int>> array1 = array;
  29. List<List<int>> array2 = array;
  30. List<List<int>> array3;//= array;
  31.  
  32. ////下面 取出二维list的某个元素
  33. //int m = array[1][2];//此时的m即为50
  34. ////下面 给二维list某位置赋值
  35. //array[1][2] = 60;
  36. ////验证某位置的值是否改变
  37. //m = array[1][2];
  38. ////将二维list的第0行所有元素给某个一维list
  39. //item = array[0];
  40.  
  41. //计时
  42. Stopwatch sw = new Stopwatch();
  43. int s = ;
  44.  
  45. //下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
  46. //list排序方法一
  47. sw.Start();
  48. for (int i = ; i < s; i++)
  49. {
  50. array1.Sort(delegate(List<int> p1, List<int> p2)
  51. {
  52. return p1[].CompareTo(p2[]);//按最后一个元素升序
  53. }
  54. );//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
  55. sw.Stop();
  56. }
  57. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  58.  
  59. //list排序方法二
  60. sw.Start();
  61. for (int i = ; i < s; i++)
  62. {
  63. array2.Sort((List<int> x, List<int> y) => { return x[].CompareTo(y[]); });
  64. sw.Stop();
  65. }
  66. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  67.  
  68. //list排序方法三
  69. sw.Start();
  70. for (int i = ; i < s; i++)
  71. {
  72. array3 = array.OrderBy(o => o[]).ToList();//升序
  73. sw.Stop();
  74. }
  75. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  76.  
  77. Console.ReadKey();
  78.  
  79. ////计时
  80. //Stopwatch sw = new Stopwatch();
  81. //sw.Start();
  82. ////Thread.Sleep(2719);
  83. //sw.Stop();
  84. //Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  85. //Console.ReadKey();
  86.  
  87. }
  88. }
  89. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. ////定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
  15. //List<List<int>> array = new List<List<int>>();
  16. ////定义一个一维list,作为上面二维list的某个元素
  17. //List<int> item = new List<int>(new int[] { 3, 4, 5, 6 });
  18. ////将上面的一维list作为一个元素放入二维list中
  19. //array.Add(item);
  20. ////给一维list赋新值
  21. //item = new List<int>(new int[] { 30, 40, 50, 60 });
  22. ////将上面的一维list作为一个元素放入二维list中
  23. //array.Add(item);
  24. ////给一维list赋新值
  25. //item = new List<int>(new int[] { 20, 40, 50, 30 });
  26. ////将上面的一维list作为一个元素放入二维list中
  27. //array.Add(item);
  28. //List<List<int>> array1 = array;
  29. //List<List<int>> array2 = array;
  30. //List<List<int>> array3;//= array;
  31.  
  32. ////下面 取出二维list的某个元素
  33. //int m = array[1][2];//此时的m即为50
  34. ////下面 给二维list某位置赋值
  35. //array[1][2] = 60;
  36. ////验证某位置的值是否改变
  37. //m = array[1][2];
  38. ////将二维list的第0行所有元素给某个一维list
  39. //item = array[0];
  40.  
  41. //计时
  42. Stopwatch sw = new Stopwatch();
  43. int s = ;
  44.  
  45. //下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
  46. //list排序方法一
  47. sw.Start();
  48. for (int i = ; i < s; i++)
  49. {
  50. //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
  51. List<List<int>> array1 = new List<List<int>>();
  52. //定义一个一维list,作为上面二维list的某个元素
  53. List<int> item = new List<int>(new int[] { , , , });
  54. //将上面的一维list作为一个元素放入二维list中
  55. array1.Add(item);
  56. //给一维list赋新值
  57. item = new List<int>(new int[] { , , , });
  58. //将上面的一维list作为一个元素放入二维list中
  59. array1.Add(item);
  60. //给一维list赋新值
  61. item = new List<int>(new int[] { , , , });
  62. //将上面的一维list作为一个元素放入二维list中
  63. array1.Add(item);
  64.  
  65. array1.Sort(delegate(List<int> p1, List<int> p2)
  66. {
  67. return p1[].CompareTo(p2[]);//按最后一个元素升序
  68. }
  69. );//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
  70. sw.Stop();
  71. }
  72. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  73.  
  74. //list排序方法二
  75. sw.Start();
  76. for (int i = ; i < s; i++)
  77. {
  78. //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
  79. List<List<int>> array2 = new List<List<int>>();
  80. //定义一个一维list,作为上面二维list的某个元素
  81. List<int> item = new List<int>(new int[] { , , , });
  82. //将上面的一维list作为一个元素放入二维list中
  83. array2.Add(item);
  84. //给一维list赋新值
  85. item = new List<int>(new int[] { , , , });
  86. //将上面的一维list作为一个元素放入二维list中
  87. array2.Add(item);
  88. //给一维list赋新值
  89. item = new List<int>(new int[] { , , , });
  90. //将上面的一维list作为一个元素放入二维list中
  91. array2.Add(item);
  92. array2.Sort((List<int> x, List<int> y) => { return x[].CompareTo(y[]); });
  93. sw.Stop();
  94. }
  95. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  96.  
  97. //list排序方法三
  98. sw.Start();
  99. for (int i = ; i < s; i++)
  100. {
  101. //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
  102. List<List<int>> array3 = new List<List<int>>();
  103. //定义一个一维list,作为上面二维list的某个元素
  104. List<int> item = new List<int>(new int[] { , , , });
  105. //将上面的一维list作为一个元素放入二维list中
  106. array3.Add(item);
  107. //给一维list赋新值
  108. item = new List<int>(new int[] { , , , });
  109. //将上面的一维list作为一个元素放入二维list中
  110. array3.Add(item);
  111. //给一维list赋新值
  112. item = new List<int>(new int[] { , , , });
  113. //将上面的一维list作为一个元素放入二维list中
  114. array3.Add(item);
  115. array3.OrderBy(o => o[]).ToList();//升序
  116. sw.Stop();
  117. }
  118. Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  119.  
  120. Console.ReadKey();
  121.  
  122. ////计时
  123. //Stopwatch sw = new Stopwatch();
  124. //sw.Start();
  125. ////Thread.Sleep(2719);//毫秒
  126. //sw.Stop();
  127. //Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  128. //Console.ReadKey();
  129.  
  130. }
  131. }
  132. }

还是第一种排序快

c# 二维list排序和计时的更多相关文章

  1. PHP二维数据排序,二维数据模糊查询

    一.因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索.三表合并后的数组结构如下: Array ( [0] => Array ( [history ...

  2. php 二维数据排序 排行榜

    php 二维数据排序 排行榜 $rateCount = array(); foreach($groupUsers as $user){ $rateCount[] = $user['rate']; } ...

  3. php对二维数据排序

    对于一维数组排序比较简单,像使用sort(),asort(),arsort()等函数进行排序,但是对于二维数组比较麻烦,所有借鉴网上的总结了一下 // 对二维数组进行指定key排序 $arr 二维数组 ...

  4. PHP一维数组和二维数字排序整理

    <?php /** 一维数组排序 sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组进行排序 ksort() - 根据键,以升 ...

  5. 稳定排序nlogn之归并排序_一维,二维

    稳定排序nlogn之归并排序_一维,二维 稳定排序:排序时间稳定的排序 稳定排序包括:归并排序(nlogn),基数排序[设待排序列为n个记录,d个关键码,关键码的取值范围为radix,则进行链式基数排 ...

  6. PHP 二维数组根据某个字段排序

    二维数组根据某个字段排序有两种办法,一种是通过sort自己写代码,一种是直接用array_multisort排序函数 一. 手写arraysort PHP的一维数组排序函数: sort  对数组的值按 ...

  7. PHP开发笔记:二维数组根据某一项来进行排序

    比如说我们现在有一个二维数组: $arr = array( ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), ‘b' => ...

  8. PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)

    一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...

  9. php对二维数组进行相关操作(排序、转换、去空白等)

    php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04   这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...

随机推荐

  1. 数据库(mysql)

    一.left join  right join  inner join left join(左连接),在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录. right join(右 ...

  2. Android Studio连接真机调试

    1.安装配置Android studio2.2 2.下载手机驱动或者安装手机助手(360手机助手) 3.用手机助手连接用于调试的手机 注意手机要开启开发者模式->允许USB调试 4.查看手机连接 ...

  3. MVc Identity登陆锁定

    2016-08-03 [ASP.NET Identity] OAuth Server 鎖定(Lockout)登入失敗次數太多的帳號 743 6 ASP.NET Identity 檢舉文章 2016-0 ...

  4. MVC之CodeFirst

    1.建立MVC项目>NuGet安装EF 2.建立模型: public class Blog { [Key] [DatabaseGenerated(DatabaseGeneratedOption. ...

  5. C语言常用关键字及运算符操作

    1.关键字 (1)数据类型 char                          1字节,8bit==256 int long,short unsgined  ,signed      无符号为 ...

  6. Oracle性能优化2- 依据场景选择技术

    1.索引的坏处 索引可以加快查询效率,但是使用不当,会造成插入性能很低 drop table test1 purge; drop table test2 purge; drop table test3 ...

  7. Service Broker 概述

    ServiceBroker(简称SSB)是基于数据库引擎提供的一个强大的异步编程模型,通过ServiceBroker,开发人员无需编写复杂的通信和消息程序,即可在数据库实例之间完成高效可靠的异步通信. ...

  8. (转)数组使用contains

    数组使用contains 今天发现一个怪问题,同样是.net3.5环境下的两个项目,一个里支持arr.contains("1"),一个就不支持,代码完全相同也不行.有时在不支持项目 ...

  9. CentOS 7.0 安装配置LAMP服务器方法(Apache+PHP+MariaDB)(转)

    转自:http://www.jb51.net/os/188488.html 作者:佚名 字体:[增加 减小] 来源:osyunwei  准备篇: CentOS 7.0系统安装配置图解教程 http:/ ...

  10. mysql 多表查询先排序,然后再取分组<mysql 先order by,然后再取group by分组>

    select * from ( select cv.lasttime,cm.mailbox,cv.clientip from `co_user_visitlog` as cv INNER JOIN ` ...