using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
List<List<int>> array = new List<List<int>>();
//定义一个一维list,作为上面二维list的某个元素
List<int> item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array.Add(item); //下面 取出二维list的某个元素
int m = array[][];//此时的m即为50
//下面 给二维list某位置赋值
array[][] = ;
//验证某位置的值是否改变
m = array[][];
//将二维list的第0行所有元素给某个一维list
item = array[]; ////下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
//list排序方法一
//array.Sort( delegate(List<int> p1,List<int> p2)
// {
// return p1[3].CompareTo(p2[3]);//按最后一个元素升序
// }
// );//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
//list排序方法二
array.Sort((List<int> x, List<int> y) => { return x[].CompareTo(y[]); });
//list排序方法三
List<List<int>> array1 = array.OrderBy(o => o[]).ToList();//升序 //计时
Stopwatch sw = new Stopwatch();
sw.Start();
Thread.Sleep();
sw.Stop();
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
Console.ReadKey(); }
}
}

二维list代替二维数组

第一种排序最快

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
List<List<int>> array = new List<List<int>>();
//定义一个一维list,作为上面二维list的某个元素
List<int> item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array.Add(item);
List<List<int>> array1 = array;
List<List<int>> array2 = array;
List<List<int>> array3;//= array; ////下面 取出二维list的某个元素
//int m = array[1][2];//此时的m即为50
////下面 给二维list某位置赋值
//array[1][2] = 60;
////验证某位置的值是否改变
//m = array[1][2];
////将二维list的第0行所有元素给某个一维list
//item = array[0]; //计时
Stopwatch sw = new Stopwatch();
int s = ; //下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
//list排序方法一
sw.Start();
for (int i = ; i < s; i++)
{
array1.Sort(delegate(List<int> p1, List<int> p2)
{
return p1[].CompareTo(p2[]);//按最后一个元素升序
}
);//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
sw.Stop();
}
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); //list排序方法二
sw.Start();
for (int i = ; i < s; i++)
{
array2.Sort((List<int> x, List<int> y) => { return x[].CompareTo(y[]); });
sw.Stop();
}
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); //list排序方法三
sw.Start();
for (int i = ; i < s; i++)
{
array3 = array.OrderBy(o => o[]).ToList();//升序
sw.Stop();
}
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); Console.ReadKey(); ////计时
//Stopwatch sw = new Stopwatch();
//sw.Start();
////Thread.Sleep(2719);
//sw.Stop();
//Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
//Console.ReadKey(); }
}
}

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
////定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
//List<List<int>> array = new List<List<int>>();
////定义一个一维list,作为上面二维list的某个元素
//List<int> item = new List<int>(new int[] { 3, 4, 5, 6 });
////将上面的一维list作为一个元素放入二维list中
//array.Add(item);
////给一维list赋新值
//item = new List<int>(new int[] { 30, 40, 50, 60 });
////将上面的一维list作为一个元素放入二维list中
//array.Add(item);
////给一维list赋新值
//item = new List<int>(new int[] { 20, 40, 50, 30 });
////将上面的一维list作为一个元素放入二维list中
//array.Add(item);
//List<List<int>> array1 = array;
//List<List<int>> array2 = array;
//List<List<int>> array3;//= array; ////下面 取出二维list的某个元素
//int m = array[1][2];//此时的m即为50
////下面 给二维list某位置赋值
//array[1][2] = 60;
////验证某位置的值是否改变
//m = array[1][2];
////将二维list的第0行所有元素给某个一维list
//item = array[0]; //计时
Stopwatch sw = new Stopwatch();
int s = ; //下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
//list排序方法一
sw.Start();
for (int i = ; i < s; i++)
{
//定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
List<List<int>> array1 = new List<List<int>>();
//定义一个一维list,作为上面二维list的某个元素
List<int> item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array1.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array1.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array1.Add(item); array1.Sort(delegate(List<int> p1, List<int> p2)
{
return p1[].CompareTo(p2[]);//按最后一个元素升序
}
);//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
sw.Stop();
}
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); //list排序方法二
sw.Start();
for (int i = ; i < s; i++)
{
//定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
List<List<int>> array2 = new List<List<int>>();
//定义一个一维list,作为上面二维list的某个元素
List<int> item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array2.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array2.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array2.Add(item);
array2.Sort((List<int> x, List<int> y) => { return x[].CompareTo(y[]); });
sw.Stop();
}
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); //list排序方法三
sw.Start();
for (int i = ; i < s; i++)
{
//定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
List<List<int>> array3 = new List<List<int>>();
//定义一个一维list,作为上面二维list的某个元素
List<int> item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array3.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array3.Add(item);
//给一维list赋新值
item = new List<int>(new int[] { , , , });
//将上面的一维list作为一个元素放入二维list中
array3.Add(item);
array3.OrderBy(o => o[]).ToList();//升序
sw.Stop();
}
Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); Console.ReadKey(); ////计时
//Stopwatch sw = new Stopwatch();
//sw.Start();
////Thread.Sleep(2719);//毫秒
//sw.Stop();
//Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
//Console.ReadKey(); }
}
}

还是第一种排序快

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. java web获取客户端外网ip和所在区域

    @参考文章1.@参考文章2.@参考文章3.@参考文章4,@之前同事的项目 controller @Controller @RequestMapping("/home") publi ...

  2. Aspose.words一 DOM结构

    2.文档对象模型概述 2.1 DOM介绍 Aspose.Words的文档对象模型(以下简称DOM)是一个Word文档在内存中的映射,Aspose.Words的DOM可以编程读取.操作和修改Word文档 ...

  3. linux通过speedtest-cli测试服务器网速

    1.git clone speedtest源码 git clone https://github.com/sivel/speedtest-cli.git 2.运行speedtest.py cd spe ...

  4. css font-size=0的妙用

    转自:css font-size=0有什么妙用? 回答一: 问题的根源是 inline(a标签默认是display:inline) 和 inline-block (.list-info 设置的是 di ...

  5. URLEncoder.encode转译后“空格”变“加号”的问题的解决方案

    我用dst_fname=URLEncoder.encode(dst_fname);对字符串dst_fname进行编码,但是发现空格全部都变成了加号,我们提需求的傻B非得要空格的,但是不编码有很多非常特 ...

  6. PAT 1066 图像过滤(15)(代码)

    1066 图像过滤(15 分) 图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式: ...

  7. Linux使用touch批量修改文件/文件夹时间戳

      Linux下touch是一个非常有用的命令. touch语法结构如下: touch [-acfm][-d <日期时间>][-r <参考文件或目录>][-t <日期时间 ...

  8. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍(转)

    原文地址:http://chenzhou123520.iteye.com/blog/1641319 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个colle ...

  9. 利用gulp搭建less编译环境

       什么是less? 一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, ...

  10. 异步Servlet和异步过虑器

    异步处理功能可以节约容器线程.此功能的作用是释放正在等待完成的线程,是该线程能够被另一请求所使用. 要编写支持异步处理的 Servlet 或者过虑器,需要设置 asyncSupported 属性为 t ...