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对 ...
随机推荐
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 数字&字符串
一.数字 数字分为整型(int)和浮点型(float) int(整型):整数数字 >>> a = 12 >>> a 12 >>> b = 12.3 ...
- webpack.prod.conf.js
// 引入依赖模块 var path = require('path') var utils = require('./utils') var webpack = require('webpack') ...
- position的absolute与fixed,absolute与relative共同点与不同点
absolute与fixed 共同点: (1) 改变行内元素的呈现方式,display被置为block: (2) 让元素脱离普通流,不占据空间: (3) 默认会覆盖到非定位元素上 不同点: absol ...
- Task.WaitAll代替WaitHandle.WaitAll
Task.Waitall阻塞了当前线程直到全完.whenall开启个新监控线程去判读括号里的所有线程执行情况并立即返回,等都完成了就退出监控线程并返回监控数据. task.Result会等待异步方法返 ...
- 梦殇 chapter six
如行尸走肉般的坐在教室,看著书上密密麻麻的字,心却久久不能平静. 自己已经不再是当初那个懵懂,无知的小女孩了.仰望天空45度,让眼泪流回眼睛. 是该告别彷徨,告别懵懂的年纪了. 早些年许下的诺言,如今 ...
- 21.Mysql Server优化
21.优化Mysql Server21.1 Mysql体系结构概览Mysql由Mysql Server层和存储引擎层组成.Mysql实例由一组后台进程.一写内存块和若干服务线程组成.Mysql后台进程 ...
- (转)jquery.cookie中的操作
jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cooki ...
- asp.net读取xml接口
//发送获取xml请求 public static string SentRequest(String url) { HttpWebRequest req = WebRequest.CreateHtt ...
- BZOJ1228或洛谷2148 [SDOI2009]E&D
BZOJ原题链接 洛谷原题链接 完全不会呀.. 写了这题才知道\(SG\)函数原来也能打表找规律... 题解请看大佬的博客 #include<cstdio> using namespace ...