查找Array的最大日期(非LINQ) [英] Find the Maximum date in an Array (non-LINQ)

查看:287
本文介绍了查找Array的最大日期(非LINQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有日期的数组

  DateTime的[] = DT新的datetime [10]; 
//填充日期
阵列



我如何找到不使用LINQ的最大日期?


解决方案

  VAR MAXDATE = DateTime.MinValue; 
的foreach(DT中的VAR日期){
MAXDATE =日期和GT; MAXDATE?日期:MAXDATE;
}

您提到使用排序算法,想必让你可以问:

  VAR MAXDATE = DT [dt.Length  -  1]; 



数组进行了排序后。关键是,你需要找到一个排序算法执行比的 O(N) 的获得任何优势更好。冒泡排序IS的 为O(n ^ 2) 的所以有在使用它是没有意义的;它执行差,平均比只通过列表运行一次。


Suppose I have an array of dates

DateTime[] dt = new DateTime[10];
// populate array with dates

How can I find the maximum date without using LINQ?

解决方案

var maxDate = DateTime.MinValue;
foreach (var date in dt) {
    maxDate = date > maxDate ? date : maxDate;
}

You mention using a sorting algorithm, presumably so that you can just ask for:

var maxDate = dt[dt.Length - 1];

After the array was sorted. The thing is, you'd need to find a sorting algorithm that performs better than O(n) to gain any advantage. Bubble Sort is O(n^2) so there's no point in using it; it performs worse, on average, than just running through the list once.

这篇关于查找Array的最大日期(非LINQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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