/// <summary>
/// 1+2+3+....+n的递归算法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public
static
int
Process1(
int
i)
{
//计算1+2+3+4+...+100的值
if
(i == 0)
return
1;
if
(i == 1)
return
1;
return
Process1(i - 2) + Process1(i - 1);
}
/// <summary>
/// 1+2+3+....+n的非递归算法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public
static
int
Process2(
int
i)
{
//计算1+2+3+4+...+100的值
if
(i == 0)
return
0;
return
Process2(i - 1) + i;
}
/// <summary>
/// 1-2+3-4+5-....+n的非递归算法
/// </summary>
/// <param name="isum"></param>
/// <param name="itype"></param>
/// <returns></returns>
public
static
int
Process0(
int
isum,
int
itype)
{
int
sum = 0;
for
(
int
i = 1; i <= isum; i++)
{
if
(itype == 1)
{
if
(i % 2 != 0)
{
sum += i;
}
else
{
sum += (-1) * i;
}
}
else
{
sum += i;
}
}
return
sum;
}
/// <summary>
/// 冒泡法排序
/// </summary>
/// <param name="arrLen"></param>
public
static
void
order1(
ref
int
[] arrLen)
{
int
temp;
for
(
int
i = 0; i < arrLen.Length; i++)
//冒泡法排序
for
(
int
j = i + 1; j < arrLen.Length; j++)
if
(arrLen[i] > arrLen[j])
{
temp = arrLen[i];
arrLen[i] = arrLen[j];
arrLen[j] = temp;
}
}
/// <summary>
/// 冒泡法排序
/// </summary>
/// <param name="arrLen"></param>
public
static
void
order2(
ref
int
[] arrLen)
{
for
(
int
i = 0; i < arrLen.Length - 1; i++)
{
for
(
int
j = 0; j < arrLen.Length - 1 - i; j++)
{
if
(arrLen[j] > arrLen[j + 1])
{
int
temp = arrLen[j];
arrLen[j] = arrLen[j + 1];
arrLen[j + 1] = temp;
}
}
}
}
//折半查找、二分算法
//数组必须按照一定的顺序
//参数:最大,最小,目标(参数类型为整数)
public
static
int
BinarySearch(
int
min,
int
max,
int
num)
{
if
(min == max)
return
-1;
int
mid = (min + max) / 2;
if
(a[mid] == num)
return
mid;
else
if
(a[mid] < num)
{
return
BinarySearch(mid + 1, max, num);
}
else
{
return
BinarySearch(min, mid - 1, num);
}
}
//折半查找、二分算法
//非递归算法
public
static
int
BinarySearch_F(
int
num)
{
int
min = 0;
int
max = 9;
int
mid;
while
(min <= max)
{
mid = (min + max) / 2;
if
(a[mid] == num)
return
mid;
else
if
(a[mid] > num) max = mid - 1;
else
min = mid + 1;
}
return
-1;
}<br>
int
a[10] ={1,5,10,13,17,23,65,77,81,93}
C# 常用算法
原文作者:五大常用算法
原文地址: https://blog.csdn.net/wangjie198704/article/details/48692333
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/wangjie198704/article/details/48692333
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。