征集!计算机算法实例

aSalt 2012-02-15 10:41:10
发这篇贴子的目的是看到很多童鞋都不太了解计算机算法,所以大家手头如果有就发出来互相学习吧。

例如什么递推、概率因子分解、拓扑排序、二叉树、二叉堆、胜者树、路径、矩阵、行列式计算、哈希表、二分/选择、冒泡、快速排序、基数排序、位图排序....加密、压缩等等(要有注解啊)。

简单的说你大学课本里那几个经典例题(如果你的书还没丢),尽量转成C#语言的。注意:别用.net里的类或C++ 里的类 。要整就整原样的,呵呵。我为什么不去C/C++里发呢,因为我根本就不会那个。

估计这个帖子会沉底……管他呢,到时候我不给分就完了,嘿嘿。
...全文
132 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ayzen1988 2012-02-16
  • 打赏
  • 举报
回复
MD5加密

public static string GetMD5String(string password)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(password));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("x2"));
}

return sb.ToString();
}
EnForGrass 2012-02-16
  • 打赏
  • 举报
回复
好吧,二分查找

class BinarySearch
{
public int search(int[] B, int p, int q, int key)
{
int result = -1;
if (p >= q && key == B[p])
{
result = p;
}
else
{
int k = (p+q)/2;
if (key == B[k])
result = k;
else
result = (key < B[k]) ? search(B, p, k-1, key) : search(B, k+1, q, key);
}
return result;
}
}
class Program
{
public static void Main()
{
Random rnd = new Random();
int[] data = new int[10];
for (int i=0; i<10; i++)
{
data[i] = i;
Console.Write("{0}\t",data[i]);
}
Console.WriteLine();
BinarySearch s = new BinarySearch();
int t = s.search(data, 0, 9, 5);
Console.Write(t);
}
}
EnForGrass 2012-02-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuanarea 的回复:]

C++快排
void sort(int *a,int x,int y)
{
int xx=x,yy=y;
int k=a[x];
if(x>=y) return ;
while(xx!=yy){
while(xx<yy&& a[yy]>=k)yy--;
a[xx]=a[yy];
while(xx<yy&am……
[/Quote]

给你改了你给分啊
熙风 2012-02-16
  • 打赏
  • 举报
回复
这个帖子是沉不了的,,,会被版主推荐的
熙风 2012-02-16
  • 打赏
  • 举报
回复

C#冒泡排序
static List<int> list = new List<int>() { 72, 54, 59, 30, 31, 78, 2, 77, 82, 72 };

static void Main(string[] args)
{
Bubble();
PrintList();
}

static void Bubble()
{
int temp = 0;
for (int i = list.Count; i > 0; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (list[j] > list[j + 1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
PrintList();
}
}

private static void PrintList()
{
foreach (var item in list)
{
Console.Write(string.Format("{0} ", item));
}
Console.WriteLine();
}




选择排序

class Program
{
static List<int> list = new List<int>() { 72, 54, 59, 30, 31, 78, 2, 77, 82, 72 };

static void Main(string[] args)
{
Choice();
PrintList();
}

static void Choice()
{
int temp = 0;
int minIndex = 0;
for (int i = 0; i < list.Count; i++)
{
minIndex = i;
for (int j = i; j < list.Count; j++)
{
//注意这里比较的是list[minIndex]
if (list[j] < list[minIndex])
{
minIndex = j;
}
}
temp = list[minIndex];
list[minIndex] = list[i];
list[i] = temp;
PrintList();
}

}

private static void PrintList()
{
foreach (var item in list)
{
Console.Write(string.Format("{0} ", item));
}
Console.WriteLine();
}

}



huangwenquan123 2012-02-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuanarea 的回复:]
C++快排
void sort(int *a,int x,int y)
{
int xx=x,yy=y;
int k=a[x];
if(x>=y) return ;
while(xx!=yy){
while(xx<yy&& a[yy]>=k)yy--;
a[xx]=a[yy];
while(xx<yy&& a[xx]<=k) xx+……
[/Quote]
        static void Main(string[] args)
{
int[] arr = { 3, 5, 2, 1, 6, 9, 7, 4, 8, 10 };
QuickSort(arr, 0, arr.Length - 1);
foreach (int i in arr)
Console.WriteLine(i);
Console.ReadLine();
}
public static void QuickSort(int[] arr,int pnLow,int pnHigh)
{
if (pnLow >= pnHigh)
return;
int PivotIndex = QuickSortOne(arr, pnLow, pnHigh);
QuickSort(arr, pnLow, PivotIndex - 1);
QuickSort(arr, PivotIndex + 1, pnHigh);
}
public static int QuickSortOne(int[] arr, int pnLow, int pnHigh)
{
int Pivot = arr[pnLow];
int i = pnLow, j = pnHigh;
while (i < j)
{
while (arr[j] >= Pivot && i < j) j--;
arr[i] = arr[j];
while (arr[i] <= Pivot && i < j) i++;
arr[j] = arr[i];
}
arr[i] = Pivot;
return i;
}
aSalt 2012-02-15
  • 打赏
  • 举报
回复
C++快排
void sort(int *a,int x,int y)
{
int xx=x,yy=y;
int k=a[x];
if(x>=y) return ;
while(xx!=yy){
while(xx<yy&& a[yy]>=k)yy--;
a[xx]=a[yy];
while(xx<yy&& a[xx]<=k) xx++;
  a[yy]=a[xx];
}   
a[xx]=k;
  sort(a,x,xx-1);
  sort(a,xx+1,y);
}
谁给我改成 C# 的??

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧