sort | algorithm C # sorting algorithm Daquan
Aborigines
2004-7-21
first, bubble sort (Bubble)
using System;
namespace Bubblesorter
{
public class Bubblesorter
{
public void Sort (int[] list)
{
int i,j,temp;
bool Done=false;
j=1;
while (j<list. Length) && (!done))
{
done=true;
for (i=0;i<list. length-j;i++)
{
if (list[i]>list[i+1])
{
Done=false;
Temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
}
}
J + +;
}
}
}
public class MainClass
{
public static void Main ()
{
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 ();
}
}
}
Second, select Sort (Selection)
using System;
namespace Selectionsorter
{
public class Selectionsorter
{
private int min;
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;
}
}
}
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<iarrary.length;m++)
Console.Write ("{0}", Iarrary[m]);
Console.WriteLine ();
}
}
}
Three, insert sort (insertionsorter)
using System;
namespace Insertionsorter
{
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,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 ();
}
}
}
Four, Hill sort (shellsorter)
using System;
namespace Shellsorter
{
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,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 ();
}
}
}