거품 양 방향 정렬

전재 출처 를 밝 혀 주 십시오:http://blog.csdn.net/droyon/article/details/8785903
/**
 *       ,        ,    ,        ,    。    。
 * @author 
 *
 */
public class BubbleTwoSort {
	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("
"); bubbingTwoSort(); printArray(); } private static void bubbingTwoSort(){ int temp; int left=0; int right=array.length-1; int index=0; while(left <= right){ index++; for(int i=left;i<right;i++){ if(array[i]>array[i+1]){ temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } } // , /*System.out.println(" "+index+" "); printArray(); System.out.println();*/ right--; for(int j=right;j>left;j--){ if(array[j]<array[j-1]){ temp = array[j]; array[j]=array[j-1]; array[j-1] = temp; } } left++; // , /*System.out.println(" "+index+" "); printArray(); System.out.println();*/ } } 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   

좋은 웹페이지 즐겨찾기