1。冒泡排序算法(Bubble Sort)
冒泡排序基本思想
将n个记录看作按纵向排列,每趟排序时自下至上对每对相邻记录进行比较,若次序不符合要求(逆序)就交换。每趟排序结束时都能使排序范围内关键字最小的记录象一个气泡一样升到表上端的对应位置,整个排序过程共进行n-1趟,依次将关键字最小、次小、第三小…的各个记录“冒到”表的第一个、第二个、第三个…位置上。
初态 第1趟 第2趟 第3趟 第4趟 第5趟 第6趟 第7趟
38 12 12 12 12 12 12 12
20 38 20 20 20 20 20 20
46 20 38 25 25 25 25 25
38 46 25 38 38 38 38 38
74 38 46 38 38 38 38 38
91 74 38 46 46 46 46 46
12 91 74 74 74 74 74 74
25 25 91 91 91 91 91 91
int[] myArray = { 9, 7, 6, 8, 5, 4 };
int itemp;
int arrLength = myArray.Length - 1;
bool isExchange = false;
for (int i = 0; i < arrLength; i++)
{
isExchange = false;
for (int j = arrLength; j >i; j--)
{
if (myArray[j - 1] > myArray[j])
{
itemp = myArray[j];
myArray[j] =myArray[j-1];
myArray[j - 1] = itemp;
isExchange = true;
}
}
if (!isExchange) //本趟排序未发生交换,提前终止算法
{ break; }
}
label1.Text = "";
foreach (int strSort in myArray)
{
label1.Text = label1.Text + strSort.ToString() + ",";
//Response.Write(strSort.ToString() + ",");
}
2.字母排序
把"baby,abc,decf,eza,ef, Abc, dec"排序成"abc,Abc,baby,dec,decf,ef,eza"
规则1.按照字母的升序排列。
2.相同的字母,大写字母放在小写字母后面。
3.前一位字母大小写相同,按后一位字母比较大小。

Code
protected void Button1_Click(object sender, EventArgs e)
{
string[] sortArr = new string[] { "baby", "abc", "decf","eza","ef", "Abc", "dec" };
string temp;
Boolean exchange; //交换标志
for (int i = 0; i < sortArr.Length-1; i++)
{
exchange = false;
for (int j = sortArr.Length-1; j > i; j--)
{
if (IsGreater(sortArr[j-1],sortArr[j]))
{
temp=sortArr[j];
sortArr[j]=sortArr[j-1];
sortArr[j-1]=temp;
exchange = true;
}
}
if (!exchange)
{
break;
}
}
foreach (string strSort in sortArr)
{
Response.Write(strSort.ToString() + ",");
}
}
比较的类

Code
static Boolean IsGreater(string strA,string strB)
{
int compareCount;//取字符串最短的长度
int asciiA;
int asciiB;
if (strA.Length > strB.Length)
{
compareCount = strB.Length;
}
else
{
compareCount = strA.Length;
}
for (int i = 0; i < compareCount; i++)
{
if (strA[i] == strB[i])
{
}
else
{
if (strA[i] < 97) //大写字母
{
asciiA = strA[i] + 32; //转化成小写字母
}
else
{
asciiA = strA[i];
}
if (strB[i] < 97) //大写字母
{
asciiB = strB[i] + 32; //转化成小写字母
}
else
{
asciiB = strB[i];
}
if (asciiA > asciiB)
{
return true;
}
else
{
return false;
}
}
}
if (strA.Length > strB.Length)
{
return true;
}
else
{
return false;
}
}
2.17个人围成一圈,从第一个人开始报数,报到3的退出,一直到剩下最后一个人

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Person
{
private int _index;
private int _id;
public Person(int index, int id)
{
this._index = index;
this._id = id;
}
public int Index
{
get { return _index; }
set { _index = value; }
}
public int Id
{
get { return _id; }
set { _id = value; }
}
}
}

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Person> personList = new List<Person>();
//初始化,添加数据
for (int i = 1; i <= 17; i++)
{
personList.Add(new Person(i,i));
}
int n = 0;
while(personList.Count>=1)
{
List<int> removeList = new List<int>();
for (int j=1;j <=personList.Count; j++)
{
if ((j+n) % 3 == 0)
{
int id = personList[j - 1].Id;
Console.WriteLine(id.ToString());
removeList.Add(j);
}
}
if (removeList.Count == 0)
{
//不够三个数的时候
n =n+ personList.Count;
}
else
{
n = personList.Count - removeList[removeList.Count - 1];
RemoveId(removeList, personList);
}
}
Console.ReadLine();
}
static void RemoveId(List<int> removeList,List<Person> personList)
{
int i = 1;
foreach (int r in removeList)
{
personList.RemoveAt(r-i);
i = i + 1;
}
}
}
}
3.一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。(斐波那契数列)

Code
using System;
public class MainClass
{
public static void Main()
{
Console.WriteLine(Foo(30));
Console.ReadLine();
}
public static int Foo(int i)
{
if (i <= 0)
{
return 0;
}
else
{
if (i > 0 && i <= 2)
{
return 1;
}
else
{
return Foo(i - 1) + Foo(i - 2);
}
}
}
}