排序算法
- 直接插入排序:
例1:
int[] param1 = { 23,44,12,11,2};
int a=0;
for (int i=0;i<=3;i++)
{
for (int j=i+1;j<=4;j++)
{
if (param1[i]>param1[j])
{
a=param1[i];
param1[i]=param1[j];
param1[j]=a;
}
}
}
例2:
using System;
public class InsertionSorter
{
public void Sort(int [] list)
{
for(int i=1;i<list.Length;++i)
{
int t=list[i];
int j=i;
while((j>0)&&(list[j-1]>t))
{
list[j]=list[j-1];
--j;
}
list[j]=t;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
InsertionSorter ii=new InsertionSorter();
ii.Sort(iArrary);
for(int m=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
}
}
- 如果数字比较多,你可以采用其它的算法,如二分法插入排序、冒泡排序等,一般讲算法的书中都有介绍。下面给出二分法的排序算法:(不限于5个数字)
public void BinarySort(ref int[] intArray)
{
int n=intArray.Length;
for(int i=1; i<n;i++)
{
int temp=intArray[i];
int left=0;
int right=i;
while(left<right)
{
int mid=(left+right)/2;
if(temp>=intArray[mid])
left=mid+1;
else
right=mid;
}
for(int j=i; j>left;j--)
{
temp=intArray[j];
intArray[j]=intArray[j-1];
intArray[j-1]=temp;
}
}
}
- 选择排序
using System;
public class SelectionSorter
{
// public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
private int min;
// private int m=0;
public void Sort(int [] list)
{
for(int i=0;i<list.Length-1;++i)
{
min=i;
for(int j=i+1;j<list.Length;++j)
{
if(list[j]<list[min])
min=j;
}
int t=list[min];
list[min]=list[i];
list[i]=t;
// Console.WriteLine("{0}",list[i]);
}
}
}
public class MainClass
{
public static void Main()
{
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<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
//SelectionSorter.Sort(iArrary);
}
}
已经编译通过。
- 希尔排序
希尔排序是将组分段,进行插入排序
public class ShellSorter
{
public void Sort(int [] list)
{
int inc;
for(inc=1;inc<=list.Length/9;inc=3*inc+1);
for(;inc>0;inc/=3)
{
for(int i=inc+1;i<=list.Length;i+=inc)
{
int t=list[i-1];
int j=i;
while((j>inc)&&(list[j-inc-1]>t))
{
list[j-1]=list[j-inc-1];
j-=inc;
}
list[j-1]=t;
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
ShellSorter sh=new ShellSorter();
sh.Sort(iArrary);
for(int m=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
}
}
已经编译通过.
- 快速排序
using System;
namespace ConsoleApplication7
{
public class Class1
{
public static void QuickSort (int[] intArray, int nLower, int nUpper)
{
if (nLower < nUpper)
{
int nSplit = Partition (intArray, nLower, nUpper);
QuickSort (intArray, nLower, nSplit - 1);
QuickSort (intArray, nSplit + 1, nUpper);
}
}
static int Partition (int[] intArray, int nLower, int nUpper)
{
int nLeft = nLower + 1;
int nPivot = intArray[nLower];
int nRight = nUpper;
int nSwap;
while (nLeft <= nRight)
{
while (nLeft <= nRight && intArray[nLeft] < nPivot)
nLeft++;
while (nLeft <= nRight && intArray[nRight] >=nPivot )
nRight--;
if (nLeft < nRight)
{
nSwap = intArray[nLeft];
intArray[nLeft] = intArray[nRight];
intArray[nRight] = nSwap;
nLeft++;
nRight--;
}
}
// Move pivot element
nSwap = intArray[nLower];
intArray[nLower] = intArray[nRight];
intArray[nRight] = nSwap;
return nRight;
}
}
public class MainClass
{
static void Main(string[] args)
{
int[] a= new int[9] {8,6,3,5,4,9,1,2,7};
Class1.QuickSort(a,0,a.Length-1);
foreach (int i in a)
Console.WriteLine(i);
Console.ReadLine();
}
}
}

浙公网安备 33010602011771号