2012-04-03 2 views
1

을 통해 배열을 인쇄하는 중 메신저는 각 패스 후에 배열을 인쇄 할 방법을 찾고 있습니다. 이것은 내가 지금까지 가지고있는 정렬 코드입니다. 어레이의 원래 상태와 분류 상태각 패스 (Bubble Sort)

public class bubbleSortTest 
{ 
    public static void main(String a[]) 
    { 
     int i; 
     int array[] = {90, 8, 7, 56, 123, 235, 9, 1, 653}; 

     System.out.println("Values Before the sort:\n"); 

     for(i = 0; i < array.length; i++) 
      System.out.print(array[i]+" "); 
      System.out.println(); 
      bubble_srt(array, array.length); 
      System.out.print("Values after the sort:\n"); 
     for(i = 0; i <array.length; i++) 
     System.out.print(array[i]+" "); 
     System.out.println(); 
     System.out.println("PAUSE"); 
    } 

    public static void bubble_srt(int a[], int n) 
    { 
     int i, j,t=0; 
      for(i = 0; i < n; i++) 
       { 
       for(j = 1; j < (n-i); j++) 
        { 
         if(a[j-1] > a[j]) 
          { 
           t = a[j-1]; 
           a[j-1]=a[j]; 
           a[j]=t; 
          } 

        } 
       } 
    } 
} 

답변

0

를 출력 버블 소트 알고리즘의 기본 구현은 각 패스의 값을 출력하는 정렬 외부 루프 내부 루프가 새롭게 추가한다.

for(i = 0; i < n; i++) 
     { 
     System.out.print(" After "+(i+1)+"st pass: "); 
     for(k=0;k<n;k++) 
      System.out.print(" "+a[k]); 
      ............... 
      ............... 
+0

내부 루프에 '교환'이 없으면 목록이 정렬되었음을 나타냅니다. 따라서 프로세스 정렬을 중단 할 수 있습니다. 다양한 정렬 알고리즘은 다음을 참조하십시오. http://www.sorting-algorithms.com/bubble-sort –