选择排序:稳定
适用于:数据量不大,并且对稳定性有要求,基本有序的情况。
public class BubbleSort{ public static void main(String[] args) { int[] a= {5,4,9,8,7,6,0,1,3,2,1}; bubbleSort(a); System.out.print(Arrays.toString(a)); }
//冒泡排序 public static void bubbleSort(int[] a) {
//设置标志位,若标志位为false表示数据已经有序,排序结束 boolean flage=false;
//i表示的循环的趟数 for(int i=0;i<a.length-1;i++) {
//j表示一趟中比较的次数 for(int j=0;j<a.length-1-i;j++) { if(a[j]>a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; flage=true; } } if(flage=false) break; } }
}
转载于:https://www.cnblogs.com/sgbe/p/10766286.html