`
zhangb5228509
  • 浏览: 5127 次
社区版块
存档分类
最新评论

三种简单的排序,冒泡、选择、插入

阅读更多

 

public class Sort {

public static void main(String[] args){

int[] bubbleArray={3,2,9,3,8,10,13,25,27,21,6,8,9,5};

bubbleArray=bubbleSort(bubbleArray);

/*

* 冒泡排序过程打印:

*  2,3,9,3,8,10,13,25,27,21,6,8,9,5

2,3,3,9,8,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,21,27,6,8,9,5

2,3,3,8,9,10,13,25,21,6,27,8,9,5

2,3,3,8,9,10,13,25,21,6,8,27,9,5

2,3,3,8,9,10,13,25,21,6,8,9,27,5

2,3,3,8,9,10,13,25,21,6,8,9,5,27

2,3,3,8,9,10,13,21,25,6,8,9,5,27

2,3,3,8,9,10,13,21,6,25,8,9,5,27

2,3,3,8,9,10,13,21,6,8,25,9,5,27

2,3,3,8,9,10,13,21,6,8,9,25,5,27

2,3,3,8,9,10,13,21,6,8,9,5,25,27

2,3,3,8,9,10,13,6,21,8,9,5,25,27

2,3,3,8,9,10,13,6,8,21,9,5,25,27

2,3,3,8,9,10,13,6,8,9,21,5,25,27

2,3,3,8,9,10,13,6,8,9,5,21,25,27

2,3,3,8,9,10,6,13,8,9,5,21,25,27

2,3,3,8,9,10,6,8,13,9,5,21,25,27

2,3,3,8,9,10,6,8,9,13,5,21,25,27

2,3,3,8,9,10,6,8,9,5,13,21,25,27

2,3,3,8,9,6,10,8,9,5,13,21,25,27

2,3,3,8,9,6,8,10,9,5,13,21,25,27

2,3,3,8,9,6,8,9,10,5,13,21,25,27

2,3,3,8,9,6,8,9,5,10,13,21,25,27

2,3,3,8,6,9,8,9,5,10,13,21,25,27

2,3,3,8,6,8,9,9,5,10,13,21,25,27

2,3,3,8,6,8,9,5,9,10,13,21,25,27

2,3,3,6,8,8,9,5,9,10,13,21,25,27

2,3,3,6,8,8,5,9,9,10,13,21,25,27

2,3,3,6,8,5,8,9,9,10,13,21,25,27

2,3,3,6,5,8,8,9,9,10,13,21,25,27

2,3,3,5,6,8,8,9,9,10,13,21,25,27

*/

int[] selectionArray={3,2,9,3,8,10,13,25,27,21,6,8,9,5};

selectionArray=selectionSort(selectionArray);

/*

选择排序过程打印:

2,3,9,3,8,10,13,25,27,21,6,8,9,5

2,3,9,3,8,10,13,25,27,21,6,8,9,5

2,3,3,9,8,10,13,25,27,21,6,8,9,5

2,3,3,5,8,10,13,25,27,21,6,8,9,9

2,3,3,5,6,10,13,25,27,21,8,8,9,9

2,3,3,5,6,8,13,25,27,21,10,8,9,9

2,3,3,5,6,8,8,25,27,21,10,13,9,9

2,3,3,5,6,8,8,9,27,21,10,13,25,9

2,3,3,5,6,8,8,9,9,21,10,13,25,27

2,3,3,5,6,8,8,9,9,10,21,13,25,27

2,3,3,5,6,8,8,9,9,10,13,21,25,27

2,3,3,5,6,8,8,9,9,10,13,21,25,27

2,3,3,5,6,8,8,9,9,10,13,21,25,27*/

int[] insertionArray={3,2,9,3,8,10,13,25,27,21,6,8,9,5};

insertionArray=insertionSort(insertionArray);

/*

插入排序过程打印

2,3,9,3,8,10,13,25,27,21,6,8,9,5

2,3,9,3,8,10,13,25,27,21,6,8,9,5

2,3,3,9,8,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,25,27,21,6,8,9,5

2,3,3,8,9,10,13,21,25,27,6,8,9,5

2,3,3,6,8,9,10,13,21,25,27,8,9,5

2,3,3,6,8,8,9,10,13,21,25,27,9,5

2,3,3,6,8,8,9,9,10,13,21,25,27,5

2,3,3,5,6,8,8,9,9,10,13,21,25,27

*/

}

/**

* 冒泡排序,按升序排

*/

public static int[] bubbleSort(int[] array){

int maxPoint=(array.length-1);

int t;

for(int i=maxPoint;i>1;i--){

for(int j=0;j<i;j++){

if(array[j]>array[j+1]){

t=array[j];

array[j]=array[j+1];

array[j+1]=t;

System.out.println(printArrayStr(array));

}

}

}

return array;

}

/**

*  选择排序,跟冒泡相比减少了交换次数

*/

public static int[] selectionSort(int[] array){

int min,t;

for(int i=0;i<(array.length-1);i++){

min=i;

for(int j=i;j<array.length;j++){

if(array[j]<array[min]){

min=j;

}

}

t=array[i];

array[i]=array[min];

array[min]=t;

System.out.println(printArrayStr(array));

}

return array;

}

/**

* 插入排序

*/

public static int[] insertionSort(int[] array){

int i,j,t;

for(i=1;i<array.length;i++){

t=array[i];

j=i;

while(j>0&&array[j-1]>=t){

array[j]=array[j-1];

j--;

}

array[j]=t;

System.out.println(printArrayStr(array));

}

return array;

}

public static String printArrayStr(int[] array){

StringBuffer str=new StringBuffer();

for(int i=0;i<(array.length);i++){

str.append(array[i]);

if(i==(array.length-1)) break;

str.append(",");

}

return str.toString();

}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics