c#中的排序数据算法 [英] sort data algorithm in c#

查看:90
本文介绍了c#中的排序数据算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜。我在地理位置上有很多Lng和Lat的数据。

如何找到max,min Lng和max,min Lat?



我用它来找到那些:



有没有快速算法可以找到它?



< pre lang =c#> double maxLng = double .MinValue,maxLat = .MinValue;
double minLng = double .MaxValue,minLat = .MaxValue;
foreach var item in 积分)
{

}



foreach (< span class =code-keyword> var
item in points)
{
maxLng = item.Lng > maxLng? item.Lng:maxLng;
maxLat = item.Lat > maxLat? item.Lat:maxLat;
minLng = item.Lng < minLng? item.Lng:minLng;
minLat = item.Lat < minLat? item.Lat:minLat;
}





还有一个数据样本:

点[ 1]:{Lat = 35.7159323908571,Lng = 51.6639151177448}

point [2]:{Lat = 35.8288428540831,Lng = 51.5313353924282}

point [3]:{Lat = 35.8210003633366,Lng = 51.2716503558998}和...

谢谢。

解决方案

你的算法最适合1次问题,它是蛮力,但你告诉我们没有别的方法。

Nota:只需删除空循环。



任何东西都可以不同取决于:

- 坐标数

- 你需要最少的次数,最多

- 它是一个随时间演变的数据库

- 这个数据的其他用法是什么

- 每次都是持久性数据还是新设置。


我建​​议make使用Linq。您可以使用以下代码作为参考



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 ConsoleApplication1
{
public class Point
{
public Double Lat { get ; set ; }
public Double Lng { get ; set ; }
}

class 程序
{
static void Main( string [] args)
{
List< ;点和GT; lstPoints = new 列表< Point>
{
new Point(){Lat = 35 7159323908571 ,Lng = 51 6639151177448 },
new Point(){Lat = 35 8288428540831 ,Lng = 51 5313353924282 },
new Point(){Lat = 35 8210003633366 ,Lng = 51 2716503558998 }
};

Double MaxLat = lstPoints.OrderByDescending(x = > x.Lat )。首先()纬度。
Double MinLat = lstPoints.OrderByDescending(x = > x.Lat).Last( ).Lat;

Double MaxLng = lstPoints.OrderByDescending(x = > x.Lng )。首先()LNG。
Double MinLng = lstPoints.OrderByDescending(x = > x.Lng).Last( ).Lng;

Console.WriteLine( Max Lat:{0},Min Lat {1} ,MaxLat,MinLat);
Console.WriteLine( Max Lng:{0},Min Lng:{1},MaxLng,MinLng);

Console.ReadLine();

}

}
}


hi. I have a lot of data of Lng and Lat in geography.
how can I find max,min Lng and max,min Lat?

I use this to find those :

is there any fast algorithm to find that?

double maxLng = double.MinValue, maxLat = double.MinValue;
          double minLng = double.MaxValue, minLat = double.MaxValue;
          foreach (var item in points)
          {

          }



          foreach (var item in points)
          {
              maxLng = item.Lng > maxLng ? item.Lng : maxLng;
              maxLat = item.Lat > maxLat ? item.Lat : maxLat;
              minLng = item.Lng < minLng ? item.Lng : minLng;
              minLat = item.Lat < minLat ? item.Lat : minLat;
          }



also there is a sample of data :
point[1]:{Lat=35.7159323908571, Lng=51.6639151177448}
point[2]:{Lat=35.8288428540831, Lng=51.5313353924282}
point[3]:{Lat=35.8210003633366, Lng=51.2716503558998} and ...
thanks.

解决方案

Your algorithm is the best for a 1 time problem, it is brute force, but there is no other way with what you told us.
Nota: just remove the empty loop.

Anything can be different depending on:
- number of coordinates
- number of times you need min, max
- is it a database that evolve with time
- what is the other usages of this data
- is it persistent data or new set every times.


I suggest make use of Linq. You may use the following code for your reference

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Point
    {
        public Double Lat { get; set; }
        public Double Lng { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Point> lstPoints = new List<Point>
            {
                new Point(){Lat=35.7159323908571, Lng=51.6639151177448},
                new Point(){Lat=35.8288428540831, Lng=51.5313353924282},
                new Point(){Lat=35.8210003633366, Lng=51.2716503558998}
            };

            Double MaxLat = lstPoints.OrderByDescending(x => x.Lat).First().Lat;
            Double MinLat = lstPoints.OrderByDescending(x => x.Lat).Last().Lat;

            Double MaxLng = lstPoints.OrderByDescending(x => x.Lng).First().Lng;
            Double MinLng = lstPoints.OrderByDescending(x => x.Lng).Last().Lng;

            Console.WriteLine("Max Lat:{0},Min Lat{1}", MaxLat, MinLat);
            Console.WriteLine("Max Lng:{0},Min Lng:{1}", MaxLng, MinLng);

            Console.ReadLine();

        }

    }
}


这篇关于c#中的排序数据算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆