给你看一个,冒泡排序只适合少量数据的排序,对多的并不推荐采用,冒泡就如它的名字的意思一样,其原理就是把轻的元素先冒泡飘出来,示例如下:
//Bubble-Sort make the Smallest to UP;
void Bubble_Sort(){
//Bubble-Sort
int count=0;
for(int i=0;i<6;i++){
for(int j=0;j<6-i;j++){
if(sortSource[j]>sortSource[j+1]){
Swap(j,j+1);
count++;
}
}
}
//end
//algorithm the worst Analize:n^2
}void Swap(int source,int dest){
int tmp;
tmp=sortSource[source];
sortSource[source]=sortSource[dest];
sortSource[dest]=tmp;
}
//Bubble-Sort make the Smallest to UP;
void Bubble_Sort(){
//Bubble-Sort
int count=0;
for(int i=0;i<6;i++){
for(int j=0;j<6-i;j++){
if(sortSource[j]>sortSource[j+1]){
Swap(j,j+1);
count++;
}
}
}
//end
//algorithm the worst Analize:n^2
}void Swap(int source,int dest){
int tmp;
tmp=sortSource[source];
sortSource[source]=sortSource[dest];
sortSource[dest]=tmp;
}
{
public void Maopao()
{}
void Bubble_Sort(int[] sortSource){
for(int i=0;i<sortSource.length;i++)
{
for(int j=0;j<sortSource.length-i-1;j++)
{
if(sortSource[j]>sortSource[j+1])
{
int tmp;
tmp = sortSource[j];
sortSource[j] = sortSource[j+1];
sortSource[j+1] = tmp;
}
}
}
for(int i=0; i<sortSource.length; i++)
{
System.out.println(sortSource[i]);
}
}public static void main(String[] arg)
{
Maopao mao = new Maopao();
int[] sortSource={44,6,8,0,3,5,7,98,0,84,6834,39,2,3,4,1,4};
mao.Bubble_Sort(sortSource);
}
}