冒泡排序
學語言要花大力氣學數據結構和算法
using System
namespace BubbleSorter
{
public class BubbleSorter
{
public void Sort(int [] list)
{
int i
bool done=false
j=
while((j<list
{
done=true
for(i=
{
if(list[i]>list[i+
{
done=false
temp=list[i]
list[i]=list[i+
list[i+
}
}
j++
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{
BubbleSorter sh=new BubbleSorter()
sh
for(int m=
Console
Console
}
}
}
選擇排序
本人用了C#開發出選擇排序算法
using System
namespace SelectionSorter
{
public class SelectionSorter
{
private int min
public void Sort(int [] list)
{
for(int i=
{
min=i
for(int j=i+
{
if(list[j]<list[min])
min=j
}
int t=list[min]
list[min]=list[i]
list[i]=t
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{
SelectionSorter ss=new SelectionSorter()
ss
for(int m=
Console
Console
}
}
}
插入排序
插入排序算法
using System
namespace InsertionSorter
{
public class InsertionSorter
{
public void Sort(int [] list)
{
for(int i=
{
int t=list[i]
int j=i
while((j>
{
list[j]=list[j
}
list[j]=t
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{
InsertionSorter ii=new InsertionSorter()
ii
for(int m=
Console
Console
}
}
}
希爾排序
希爾排序是將組分段
using System
namespace ShellSorter
{
public class ShellSorter
{
public void Sort(int [] list)
{
int inc
for(inc=
for(
{
for(int i=inc+
{
int t=list[i
int j=i
while((j>inc)&&(list[j
{
list[j
j
}
list[j
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{
ShellSorter sh=new ShellSorter()
sh
for(int m=
Console
Console
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11523.html