冒泡算法:
基本思想:两两比较待排序数据元素的大小,发现两个数据元素的次序相反时即进行交换,直到没有反序的数据元素为止。
代码实现:
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 MainClassTest

{ public static void Main()

{

int[] iArrary=new int[]{34,3,5,6,43,56,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();

}
}
运行结果:

基本思想:两两比较待排序数据元素的大小,发现两个数据元素的次序相反时即进行交换,直到没有反序的数据元素为止。
代码实现:
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 MainClassTest 
{ public static void Main() 
{ 
int[] iArrary=new int[]{34,3,5,6,43,56,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(); 
}
}

浙公网安备 33010602011771号