1.C#堆排序代码

private static void Adjust (int[] list, int i, int m)
{
int Temp = list[i];
int j = i * 2 + 1; while (j <= m)
{
//more children
if(j < m)
if(list[j] < list[j + 1])
j = j + 1; //compare roots and the older children
if(Temp < list[j])
{
list[i] = list[j];
i = j;
j = 2 * i + 1;
}
else
{
j = m + 1;
}
} list [i] = Temp;
}
public static void HeapSort (int[] list)
{
//build the initial heap
for (int i = (list.Length - 1) / 2; i > = 0; i-)
Adjust (list, i, list.Length - 1); //swap root node and the last heap node
for (int i = list.Length - 1; i > = 1; i-)
{
int Temp = list [0];
list [0] = list [i];
list [i] = Temp;
Adjust (list, 0, i - 1);
}
}

2.快速排序

private static int Partition (int[] list, int i, int j)
{
int Key = list [i]; while (i < j)
{
//j to the left scan
while (list [j] >= Key && i < j)
j--; if(i< j)
list [i++] = list [j]; //i to the right scan
while (list [i] <= Key && i < j)
i++; IF (i < j)
list [j--] = list[i];
} list [i] = Key;
return i;
}
public static void QuickSort (int[] list, int low, int high)
{
if(low < high - 1)
{
int Key = Partition (list, low, high);
QuickSort (list, low, Key - 1);
QuickSort (list, Key + 1, high);
}
}

3.冒泡排序

    public static void BubbleSort (int[] list)
{
for (int i = 0; i < list.Length; i++)
{
for (int j = 0; j < list.Length - i - 1; j++)
{
if(list [j] > list [j + 1])
{
int Temp = list [j];
list [j] = list [j + 1];
list [j + 1] = Temp;
}
}
}
}

4.选择排序

public static void SelectSort (int[] list)
{
for (int i = 0; i < list.Length; i++)
{
int min = i;
for (int j = i + 1; j < list.Length; j++)
if(list [j] < list [min])
min = j; if(min ! = i)
{
int Temp = list [min];
list [min] = list [i];
list [i] = Temp;
}
}
}

5.二分查找

public int FindPosition(int num, int[] arr)
{
int left = 0;
int right = arr.Length - 1; while (left < right - 1)
{
if (arr[left] == num)
{
return left;
}
if (arr[right] == num)
{
return right;
} int middle = (left + right) / 2;
if (num == arr[middle])
{
return middle;
}
else if (num < arr[middle])
{
right = middle;
}
else
{
left = middle;
} }
return -1;
}

6.C#公历转农历

/// <summary>
/// LunDay 的摘要说明。
/// 用法说明
/// 直接调用即可,比较简单
/// </summary>
public class LunDay
{
public LunDay()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//天干
private static string[] TianGan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; //地支
private static string[] DiZhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; //十二生肖
private static string[] ShengXiao = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; //农历日期
private static string[] DayName = {"*","初一","初二","初三","初四","初五",
"初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五",
"十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五",
"廿六","廿七","廿八","廿九","三十"}; //农历月份
private static string[] MonthName = { "*", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊" }; //公历月计数天
private static int[] MonthAdd = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
//农历数据
private static int[] LunarData = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438
,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};
/// <summary>
/// 获取对应日期的农历
/// </summary>
/// <param name="dtDay">公历日期</param>
/// <returns></returns>
public string GetLunarCalendar(DateTime dtDay)
{
string sYear = dtDay.Year.ToString();
string sMonth = dtDay.Month.ToString();
string sDay = dtDay.Day.ToString();
int year;
int month;
int day;
try
{
year = int.Parse(sYear);
month = int.Parse(sMonth);
day = int.Parse(sDay);
}
catch
{
year = DateTime.Now.Year;
month = DateTime.Now.Month;
day = DateTime.Now.Day;
} int nTheDate;
int nIsEnd;
int k, m, n, nBit, i;
string calendar = string.Empty;
//计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
nTheDate = (year - 1921) * 365 + (year - 1921) / 4 + day + MonthAdd[month - 1] - 38;
if ((year % 4 == 0) && (month > 2))
nTheDate += 1;
//计算天干,地支,月,日
nIsEnd = 0;
m = 0;
k = 0;
n = 0;
while (nIsEnd != 1)
{
if (LunarData[m] < 4095)
k = 11;
else
k = 12;
n = k;
while (n >= 0)
{
//获取LunarData[m]的第n个二进制位的值
nBit = LunarData[m];
for (i = 1; i < n + 1; i++)
nBit = nBit / 2;
nBit = nBit % 2;
if (nTheDate <= (29 + nBit))
{
nIsEnd = 1;
break;
}
nTheDate = nTheDate - 29 - nBit;
n = n - 1;
}
if (nIsEnd == 1)
break;
m = m + 1;
}
year = 1921 + m;
month = k - n + 1;
day = nTheDate;
//return year+"-"+month+"-"+day; #region 格式化日期显示为三月廿四
if (k == 12)
{
if (month == LunarData[m] / 65536 + 1)
month = 1 - month;
else if (month > LunarData[m] / 65536 + 1)
month = month - 1;
} //生肖
calendar = ShengXiao[(year - 4) % 60 % 12].ToString() + "年 ";
//天干
calendar += TianGan[(year - 4) % 60 % 10].ToString();
//地支
calendar += DiZhi[(year - 4) % 60 % 12].ToString() + " "; //农历月
if (month < 1)
calendar += "闰" + MonthName[-1 * month].ToString() + "月";
else
calendar += MonthName[month].ToString() + "月"; //农历日
calendar += DayName[day].ToString() + "日"; return calendar; #endregion
}
}

C#常用排序和查找算法的更多相关文章

  1. 常用的STL查找算法

    常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...

  2. C# 基础排序与查找算法

    排序算法: class Sort { static void swap<T>(ref T a, ref T b) { T tmp = a; a = b; b = tmp; } #regio ...

  3. 基于python常用排序与查找

    """ 排序与查找 -- 冒泡排序 -- 选择排序 -- 快速排序 --****经典 -- 希尔排序 """ # 常用排序的实现 # 冒泡排 ...

  4. [PHP] 排序和查找算法

    知乎:冒泡排序(bubble sort)的原理是什么? 潘屹峰: 冒泡排序的原理可以顾名思义:把每个数据看成一个气泡,按初始顺序自底向上依次对两两气泡进行比较,对上重下轻的气泡交换顺序(这里用气泡轻. ...

  5. 面试常问的几个排序和查找算法,PHP实现

    冒泡,快排,二分查找,都是面试常问的几个算法题目,虽然简单,但是一段时间不用的话就很容易忘记,这里我用PHP实现了一下,温故而知新. 排序 冒泡排序 每一次冒出一个最大的值 function bubb ...

  6. python基础一 ------排序和查找算法

    插入排序; 假设数组长度为n,先从第二个元素开始,与前一个元素比较,之后将较小的元素    放在前面,现在前两个元素是有顺序的,这时取第三个元素,与前一个元素(也就是第二个)比较,较小的放在前面   ...

  7. python 排序和查找算法

    一.搜索 1.顺序查找 数据存储在具有线性或顺序关系的结构中时,可顺序访问查找 def sequential_search(ilist, item): pos = 0 while pos < l ...

  8. javascript排序 查找算法大全

    在pptv的实习结束了, 忙着找工作的事,顺便把数据结构的那本书重新复习了一遍.为了加深印象,特意把里面的常用的排序.查找算法用js写了一遍 具体的实例在我的github上,大家可以访问的: http ...

  9. Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...

随机推荐

  1. 04-oracle时间函数

    add_months(sysdate,x)x月之后的日期:last_day(sysdate)指定日期所在月份的最后一天:next_day(sysdate,'星期x')当前日期后的下一个星期x: mon ...

  2. C#反射动态调用dll中的方法,并返回结果(转)

    反射的作用是动态的加载某个dll(程序集),并执行该程序集中的某个方法,并返回结果:当然也可以给该方法传递参数 namespace assembly_name { public class assem ...

  3. redis3.0 cluster功能介绍

    edis从3.0开始支持集群功能.redis集群采用无中心节点方式实现,无需proxy代理,客户端直接与redis集群的每个节点连接,根据同样的hash算法计算出key对应的slot,然后直接在slo ...

  4. window 端口占用,杀进程

    假如我们需要确定谁占用了我们的8008端口,在windows命令行窗口下执行: C:\Documents and Settings>netstat -aon|findstr 80 看到了吗,端口 ...

  5. C#操作Redis List 列表

    /// <summary> /// Redis 列表 /// </summary> public static void Redis_List() { RedisClient ...

  6. C3P0数据库连接池的java实现

    1.配置准备 导入jar包 c3p0-0.9.2-pre1.jar mchange-commons-0.2.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.j ...

  7. EV3DVue干涉检测的优势分析

    过去几年中国制造行业获得了的快速发展,各企业为了尽可能早的抢占市场,对模具的生产周期要求越来越短,精度要求越来越高,这就对模具设计以及制造等各个环节提出了更高的要求.随着CAD/CAM技术的深入应用, ...

  8. 移除TFS服务器关系

    移除TFS服务器关系:1.将项目和从以前的TFS服务器断开.2.退出VS.3.找到C:\Documents and Settings\Administrator\Local Settings\Appl ...

  9. 一:SpringMVC架构流程

    架构流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映射器根据请求url ...

  10. dubbo配置清单-超详细版

    服务发布者 在服务发布者的springboot主配置文件application.properties中添加dubbo配置 #dubbo服务名 spring.dubbo.application.name ...