首页 > 代码库 > 冒泡排序 java

冒泡排序 java

把以前学的都捡回来。

直接上代码:

public class Bubble{
	public static void main(String [] args){
		int[] arr = new int[]{2,3,1,4,5,4,2,6,0,7,21};
		int[] arrAfterSort = sort(arr,arr.length);
		for(int i = 0;i < arrAfterSort.length;i++){
			System.out.println(arrAfterSort[i]);
		}	
	}
	
	public static int[] sort(int[] array,int length){
		for(int i = 0;i < length-1;i++){
			for(int j = 0;j < length-i-1;j++){
				if(array[j]>array[j+1]){
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		}
		return array;
	}
}

  

冒泡排序 java