C#常用排序和查找算法
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#常用排序和查找算法的更多相关文章
- 常用的STL查找算法
常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
- C# 基础排序与查找算法
排序算法: class Sort { static void swap<T>(ref T a, ref T b) { T tmp = a; a = b; b = tmp; } #regio ...
- 基于python常用排序与查找
""" 排序与查找 -- 冒泡排序 -- 选择排序 -- 快速排序 --****经典 -- 希尔排序 """ # 常用排序的实现 # 冒泡排 ...
- [PHP] 排序和查找算法
知乎:冒泡排序(bubble sort)的原理是什么? 潘屹峰: 冒泡排序的原理可以顾名思义:把每个数据看成一个气泡,按初始顺序自底向上依次对两两气泡进行比较,对上重下轻的气泡交换顺序(这里用气泡轻. ...
- 面试常问的几个排序和查找算法,PHP实现
冒泡,快排,二分查找,都是面试常问的几个算法题目,虽然简单,但是一段时间不用的话就很容易忘记,这里我用PHP实现了一下,温故而知新. 排序 冒泡排序 每一次冒出一个最大的值 function bubb ...
- python基础一 ------排序和查找算法
插入排序; 假设数组长度为n,先从第二个元素开始,与前一个元素比较,之后将较小的元素 放在前面,现在前两个元素是有顺序的,这时取第三个元素,与前一个元素(也就是第二个)比较,较小的放在前面 ...
- python 排序和查找算法
一.搜索 1.顺序查找 数据存储在具有线性或顺序关系的结构中时,可顺序访问查找 def sequential_search(ilist, item): pos = 0 while pos < l ...
- javascript排序 查找算法大全
在pptv的实习结束了, 忙着找工作的事,顺便把数据结构的那本书重新复习了一遍.为了加深印象,特意把里面的常用的排序.查找算法用js写了一遍 具体的实例在我的github上,大家可以访问的: http ...
- Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法
Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...
随机推荐
- DP Intro - poj 1947 Rebuilding Roads
算法: dp[i][j]表示以i为根的子树要变成有j个节点的状态需要减掉的边数. 考虑状态转移的时候不考虑i的父亲节点,就当不存在.最后统计最少减去边数的 时候+1. 考虑一个节点时,有两种选择,要么 ...
- AngularJs学习笔记--Injecting Services Into Controllers
原版地址:http://docs.angularjs.org/guide/dev_guide.services.injecting_controllers 把service当作被依赖的资源加载到con ...
- md5码加密(Python)
import hashlib import hmac m = input('输入要加密内容:') md = hashlib.md5()#生成md5 hash对象 md.update(m.encode( ...
- 100行代码搞定抖音短视频App,终于可以和美女合唱了。
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由视频咖 发表于云+社区专栏 本文作者,shengcui,腾讯云高级开发工程师,负责移动客户端开发 最近抖音最近又带了一波合唱的节奏,老 ...
- 【c++】输出文件的每个单词、行
假设文件内容为 1. hello1 hello2 hello3 hello4 2. dsfjdosi 3. skfskj ksdfls 输出每个单词 代码 #include <iostream& ...
- Linux下ffmpeg添加Facebook/transform代码块实现将全景视频的球模型转换成立方体模型
Facebook事实上已开始在平台中支持360度全景视频的流播,但公司对此并不满足.其工程师更是基于锥体几何学设计出了一套全新的视频编码,号称最高能将全景视频的文件大小减少80%.(VR最新突破:全景 ...
- <td>标签clospan和rowspan 可横跨列数和行数
<td colspan="2"> <input type="text" name="reason_other" size= ...
- Java 线程--继承java.lang.Thread类实现线程
现实生活中的很多事情是同时进行的,Java中为了模拟这种状态,引入了线程机制.先来看线程的基本概念. 线程是指进程中的一个执行场景,也就是执行流程,进程和线程的区别: 1.每个进程是一个应用程序,都有 ...
- sql: T-SQL parent-child function script
--Parent-Child reationship --涂聚文 2014-08-25 --得位置的子節點函數表(包含本身) if exists (select * from dbo.sysobjec ...
- 001服务注册与发现Eureka
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka Server依赖和Spring Cloud依赖管理 <dependencies> <!--添加Eurek ...