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

      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 
      9 namespace ConsoleApplication1
     10 {
     11     class Program
     12     {
     13         static void Main(string[] args)
     14         {
     15             ////定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
     16             //List<List<int>> array = new List<List<int>>();
     17             ////定义一个一维list,作为上面二维list的某个元素
     18             //List<int> item = new List<int>(new int[] { 3, 4, 5, 6 });
     19             ////将上面的一维list作为一个元素放入二维list中
     20             //array.Add(item);
     21             ////给一维list赋新值
     22             //item = new List<int>(new int[] { 30, 40, 50, 60 });
     23             ////将上面的一维list作为一个元素放入二维list中
     24             //array.Add(item);
     25             ////给一维list赋新值
     26             //item = new List<int>(new int[] { 20, 40, 50, 30 });
     27             ////将上面的一维list作为一个元素放入二维list中
     28             //array.Add(item);
     29             //List<List<int>> array1 = array;
     30             //List<List<int>> array2 = array;
     31             //List<List<int>> array3;//= array;
     32 
     33 
     34 
     35             ////下面    取出二维list的某个元素
     36             //int m = array[1][2];//此时的m即为50
     37             ////下面    给二维list某位置赋值
     38             //array[1][2] = 60;
     39             ////验证某位置的值是否改变
     40             //m = array[1][2];
     41             ////将二维list的第0行所有元素给某个一维list
     42             //item = array[0];
     43 
     44 
     45             //计时
     46             Stopwatch sw = new Stopwatch();
     47             int s = 100000;
     48 
     49             //下面对二维list排序,且都是按最后一个元素排序,因为最后一个我作为遗传算法的fit值
     50             //list排序方法一
     51             sw.Start();
     52             for (int i = 0; i < s; i++)
     53             {
     54                 //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
     55                 List<List<int>> array1 = new List<List<int>>();
     56                 //定义一个一维list,作为上面二维list的某个元素
     57                 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 });
     58                 //将上面的一维list作为一个元素放入二维list中
     59                 array1.Add(item);
     60                 //给一维list赋新值
     61                 item = new List<int>(new int[] { 30, 40, 50, 60 });
     62                 //将上面的一维list作为一个元素放入二维list中
     63                 array1.Add(item);
     64                 //给一维list赋新值
     65                 item = new List<int>(new int[] { 20, 40, 50, 30 });
     66                 //将上面的一维list作为一个元素放入二维list中
     67                 array1.Add(item);
     68 
     69                 array1.Sort(delegate(List<int> p1, List<int> p2)
     70                     {
     71                         return p1[3].CompareTo(p2[3]);//按最后一个元素升序
     72                     }
     73                     );//升序或则用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821
     74                 sw.Stop();
     75             }
     76             Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
     77 
     78 
     79             //list排序方法二
     80             sw.Start();
     81             for (int i = 0; i < s; i++)
     82             {
     83                 //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
     84                 List<List<int>> array2 = new List<List<int>>();
     85                 //定义一个一维list,作为上面二维list的某个元素
     86                 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 });
     87                 //将上面的一维list作为一个元素放入二维list中
     88                 array2.Add(item);
     89                 //给一维list赋新值
     90                 item = new List<int>(new int[] { 30, 40, 50, 60 });
     91                 //将上面的一维list作为一个元素放入二维list中
     92                 array2.Add(item);
     93                 //给一维list赋新值
     94                 item = new List<int>(new int[] { 20, 40, 50, 30 });
     95                 //将上面的一维list作为一个元素放入二维list中
     96                 array2.Add(item);
     97                 array2.Sort((List<int> x, List<int> y) => { return x[3].CompareTo(y[3]); });
     98                 sw.Stop();
     99             }
    100             Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
    101 
    102             //list排序方法三
    103             sw.Start();
    104             for (int i = 0; i < s; i++)
    105             {
    106                 //定义一个二维list,用来代替二维数组,这样每行的个数就可以变了
    107                 List<List<int>> array3 = new List<List<int>>();
    108                 //定义一个一维list,作为上面二维list的某个元素
    109                 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 });
    110                 //将上面的一维list作为一个元素放入二维list中
    111                 array3.Add(item);
    112                 //给一维list赋新值
    113                 item = new List<int>(new int[] { 30, 40, 50, 60 });
    114                 //将上面的一维list作为一个元素放入二维list中
    115                 array3.Add(item);
    116                 //给一维list赋新值
    117                 item = new List<int>(new int[] { 20, 40, 50, 30 });
    118                 //将上面的一维list作为一个元素放入二维list中
    119                 array3.Add(item);
    120                 array3.OrderBy(o => o[0]).ToList();//升序
    121                 sw.Stop();
    122             }
    123             Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
    124 
    125             Console.ReadKey();
    126 
    127 
    128 
    129 
    130 
    131             ////计时
    132             //Stopwatch sw = new Stopwatch();
    133             //sw.Start();
    134             ////Thread.Sleep(2719);//毫秒
    135             //sw.Stop();
    136             //Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
    137             //Console.ReadKey();
    138 
    139         }
    140     }
    141 }
    还是第一种排序快
  • 相关阅读:
    I00038 自守数(Automorphic number)
    I00036 盈数(Abundant number)
    I00036 盈数(Abundant number)
    I00037 亏数(Deficient number)
    I00037 亏数(Deficient number)
    I00035 完美数(Perfect number)
    I00035 完美数(Perfect number)
    I00034 累加与累乘
    I00034 累加与累乘
    codeforces589J 简单dfs,队列
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/8284287.html
Copyright © 2011-2022 走看看