c# 二维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的某个元素
- 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排序和计时的更多相关文章
- PHP二维数据排序,二维数据模糊查询
一.因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索.三表合并后的数组结构如下: Array ( [0] => Array ( [history ...
- php 二维数据排序 排行榜
php 二维数据排序 排行榜 $rateCount = array(); foreach($groupUsers as $user){ $rateCount[] = $user['rate']; } ...
- php对二维数据排序
对于一维数组排序比较简单,像使用sort(),asort(),arsort()等函数进行排序,但是对于二维数组比较麻烦,所有借鉴网上的总结了一下 // 对二维数组进行指定key排序 $arr 二维数组 ...
- PHP一维数组和二维数字排序整理
<?php /** 一维数组排序 sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组进行排序 ksort() - 根据键,以升 ...
- 稳定排序nlogn之归并排序_一维,二维
稳定排序nlogn之归并排序_一维,二维 稳定排序:排序时间稳定的排序 稳定排序包括:归并排序(nlogn),基数排序[设待排序列为n个记录,d个关键码,关键码的取值范围为radix,则进行链式基数排 ...
- PHP 二维数组根据某个字段排序
二维数组根据某个字段排序有两种办法,一种是通过sort自己写代码,一种是直接用array_multisort排序函数 一. 手写arraysort PHP的一维数组排序函数: sort 对数组的值按 ...
- PHP开发笔记:二维数组根据某一项来进行排序
比如说我们现在有一个二维数组: $arr = array( ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), ‘b' => ...
- PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)
一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...
- php对二维数组进行相关操作(排序、转换、去空白等)
php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04 这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...
随机推荐
- 数据库(mysql)
一.left join right join inner join left join(左连接),在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录. right join(右 ...
- Android Studio连接真机调试
1.安装配置Android studio2.2 2.下载手机驱动或者安装手机助手(360手机助手) 3.用手机助手连接用于调试的手机 注意手机要开启开发者模式->允许USB调试 4.查看手机连接 ...
- MVc Identity登陆锁定
2016-08-03 [ASP.NET Identity] OAuth Server 鎖定(Lockout)登入失敗次數太多的帳號 743 6 ASP.NET Identity 檢舉文章 2016-0 ...
- MVC之CodeFirst
1.建立MVC项目>NuGet安装EF 2.建立模型: public class Blog { [Key] [DatabaseGenerated(DatabaseGeneratedOption. ...
- C语言常用关键字及运算符操作
1.关键字 (1)数据类型 char 1字节,8bit==256 int long,short unsgined ,signed 无符号为 ...
- Oracle性能优化2- 依据场景选择技术
1.索引的坏处 索引可以加快查询效率,但是使用不当,会造成插入性能很低 drop table test1 purge; drop table test2 purge; drop table test3 ...
- Service Broker 概述
ServiceBroker(简称SSB)是基于数据库引擎提供的一个强大的异步编程模型,通过ServiceBroker,开发人员无需编写复杂的通信和消息程序,即可在数据库实例之间完成高效可靠的异步通信. ...
- (转)数组使用contains
数组使用contains 今天发现一个怪问题,同样是.net3.5环境下的两个项目,一个里支持arr.contains("1"),一个就不支持,代码完全相同也不行.有时在不支持项目 ...
- CentOS 7.0 安装配置LAMP服务器方法(Apache+PHP+MariaDB)(转)
转自:http://www.jb51.net/os/188488.html 作者:佚名 字体:[增加 减小] 来源:osyunwei 准备篇: CentOS 7.0系统安装配置图解教程 http:/ ...
- mysql 多表查询先排序,然后再取分组<mysql 先order by,然后再取group by分组>
select * from ( select cv.lasttime,cm.mailbox,cv.clientip from `co_user_visitlog` as cv INNER JOIN ` ...