冒泡排序与优化后的冒泡

package test;

public class BubbleSort {
	public static void bubbleSort(int arr[]){
		int tmp=0;
		for(int i=0;ii;j--){
				if(arr[j]
public class BubbleSort2 {
	public static void bubbleSort2(int arr[]) {
		for(int i=0; i < arr.length-1; i++){
			boolean flag=false;
			for(int j=0; jarr[j+1]){
					int tmp=arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=tmp;
					flag=true;
				}
			}
			if(!flag) return;
		}
	}
}

 

你可能感兴趣的:(数据结构)