冒泡排序

转载请注明出处:http://blog.csdn.net/droyon/article/details/8785666

/**
 * 两两比较,小数在前,大数在后。一次排序后,最小值交换到最前面,类似冒泡。
 * o(N*N)
 * @author
 * 冒泡排序,选择排序,插入排序,交换排序属于简单排序方法
 */
public class BubbleSort {
	private static int[] array = new int[]{1,8,2,9,3,7,11,23,90,4,5};
	public static void main(String args[]){
        System.out.println("排序前");
        printArray();
        System.out.println("\n排序后");
        bubbingSort();
        printArray();
	 }
	 private static void bubbingSort(){
		 int temp;
		 for(int i=1;i<array.length;i++){//两两比较,小数上冒
			 for(int j=array.length-1;j>=i;j--){
				 if(array[j]<array[j-1]){
					 temp = array[j];
					 array[j] = array[j-1];
					 array[j-1] = temp;
				 }
			 }
		 }
	 }
	 public static void printArray(){  
        for(int i=0;i<array.length;i++){  
            System.out.print(array[i]+"   ");  
        }  
	 }  
}


运行结果:

排序前
1   8   2   9   3   7   11   23   90   4   5   
排序后
1   2   3   4   5   7   8   9   11   23   90   


你可能感兴趣的:(冒泡排序)