一:冒泡排序
using System;
![](/Images/OutliningIndicators/None.gif)
namespace BubbleSorter
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class BubbleSorter
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void Sort(int[] list)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i,j,temp;
bool done=false;
j=1;
while((j<list.Length)&&(!done))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
done=true;
for(i=0;i<list.Length-j;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(list[i]>list[i+1])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
done=false;
temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
}
}
j++;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class MainClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] iArrary=new int[]
{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
BubbleSorter sh=new BubbleSorter();
sh.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
二:选择排序
using System;
![](/Images/OutliningIndicators/None.gif)
namespace SelectionSorter
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class SelectionSorter
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int min;
public void Sort(int [] list)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=0;i<list.Length-1;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
min=i;
for(int j=i+1;j<list.Length;j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(list[j]<list[min])
min=j;
}
int t=list[min];
list[min]=list[i];
list[i]=t;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class MainClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] iArrary = new int[]
{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter ss=new SelectionSorter();
ss.Sort(iArrary);
for (int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
三:插入排序
using System;
![](/Images/OutliningIndicators/None.gif)
namespace InsertionSorter
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class InsertionSorter
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void Sort(int [] list)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=1;i<list.Length;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=list[i];
int j=i;
while((j>0)&&(list[j-1]>t))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
list[j]=list[j-1];
--j;
}
list[j]=t;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class MainClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] iArrary=new int[]
{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
InsertionSorter ii=new InsertionSorter();
ii.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0}",iArrary[m]);
Console.WriteLine();
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
四:希尔排序
using System;
![](/Images/OutliningIndicators/None.gif)
namespace ShellSorter
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class ShellSorter
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void Sort(int [] list)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int inc;
for(inc=1;inc<=list.Length/9;inc=3*inc+1);
for(;inc>0;inc/=3)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=inc+1;i<=list.Length;i+=inc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=list[i-1];
int j=i;
while((j>inc)&&(list[j-inc-1]>t))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
list[j-1]=list[j-inc-1];
j-=inc;
}
list[j-1]=t;
}
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class MainClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] iArrary=new int[]
{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
ShellSorter sh=new ShellSorter();
sh.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
![](/Images/OutliningIndicators/None.gif)
posted @
2007-09-12 12:01
sunfishlu
阅读(
249)
评论()
编辑
收藏
举报