코드
import java.util.*;
class 버블정렬 {
//뒷자리를 제일 큰 수로 채우면서 줄여간다.
//따라서 매 순회마다 뒷자리가 차곡차곡 큰 수로 채워진다.
public static void main(String[] args){
int[] arr = new Random().ints(10, 50, 100).toArray();
for(int i=0;i<arr.length-1;i++) {
System.out.println("i 순회 중 = "+ i);
for(int j=0;j<arr.length-1-i;j++) {
if(arr[j]>arr[j+1]) {
System.out.println("arr["+j+"]="+arr[j]+" <==> arr["+j+"+1]="+arr[j+1]);
for(int z=0;z<j-1;z++) System.out.print(" ");
System.out.println(" *");
System.out.println(Arrays.toString(arr));
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
System.out.println(Arrays.toString(arr));
System.out.println("========================================");
}
}
}
}
}
결과
i 순회 중 = 0
arr[0]=69 <==> arr[0+1]=58
*
[69, 58, 65, 96, 91, 95, 82, 72, 96, 85]
[58, 69, 65, 96, 91, 95, 82, 72, 96, 85]
========================================
arr[1]=69 <==> arr[1+1]=65
*
[58, 69, 65, 96, 91, 95, 82, 72, 96, 85]
[58, 65, 69, 96, 91, 95, 82, 72, 96, 85]
========================================
arr[3]=96 <==> arr[3+1]=91
*
[58, 65, 69, 96, 91, 95, 82, 72, 96, 85]
[58, 65, 69, 91, 96, 95, 82, 72, 96, 85]
========================================
arr[4]=96 <==> arr[4+1]=95
*
[58, 65, 69, 91, 96, 95, 82, 72, 96, 85]
[58, 65, 69, 91, 95, 96, 82, 72, 96, 85]
========================================
arr[5]=96 <==> arr[5+1]=82
*
[58, 65, 69, 91, 95, 96, 82, 72, 96, 85]
[58, 65, 69, 91, 95, 82, 96, 72, 96, 85]
========================================
arr[6]=96 <==> arr[6+1]=72
*
[58, 65, 69, 91, 95, 82, 96, 72, 96, 85]
[58, 65, 69, 91, 95, 82, 72, 96, 96, 85]
========================================
arr[8]=96 <==> arr[8+1]=85
*
[58, 65, 69, 91, 95, 82, 72, 96, 96, 85]
[58, 65, 69, 91, 95, 82, 72, 96, 85, 96]
========================================
i 순회 중 = 1
arr[4]=95 <==> arr[4+1]=82
*
[58, 65, 69, 91, 95, 82, 72, 96, 85, 96]
[58, 65, 69, 91, 82, 95, 72, 96, 85, 96]
========================================
arr[5]=95 <==> arr[5+1]=72
*
[58, 65, 69, 91, 82, 95, 72, 96, 85, 96]
[58, 65, 69, 91, 82, 72, 95, 96, 85, 96]
========================================
arr[7]=96 <==> arr[7+1]=85
*
[58, 65, 69, 91, 82, 72, 95, 96, 85, 96]
[58, 65, 69, 91, 82, 72, 95, 85, 96, 96]
========================================
i 순회 중 = 2
arr[3]=91 <==> arr[3+1]=82
*
[58, 65, 69, 91, 82, 72, 95, 85, 96, 96]
[58, 65, 69, 82, 91, 72, 95, 85, 96, 96]
========================================
arr[4]=91 <==> arr[4+1]=72
*
[58, 65, 69, 82, 91, 72, 95, 85, 96, 96]
[58, 65, 69, 82, 72, 91, 95, 85, 96, 96]
========================================
arr[6]=95 <==> arr[6+1]=85
*
[58, 65, 69, 82, 72, 91, 95, 85, 96, 96]
[58, 65, 69, 82, 72, 91, 85, 95, 96, 96]
========================================
i 순회 중 = 3
arr[3]=82 <==> arr[3+1]=72
*
[58, 65, 69, 82, 72, 91, 85, 95, 96, 96]
[58, 65, 69, 72, 82, 91, 85, 95, 96, 96]
========================================
arr[5]=91 <==> arr[5+1]=85
*
[58, 65, 69, 72, 82, 91, 85, 95, 96, 96]
[58, 65, 69, 72, 82, 85, 91, 95, 96, 96]
========================================
i 순회 중 = 4
i 순회 중 = 5
i 순회 중 = 6
i 순회 중 = 7
i 순회 중 = 8
'자료구조&알고리즘 > 자바(Java) 알고리즘 문제풀이 : 코딩테스트 대비' 카테고리의 다른 글
장난꾸러기 (0) | 2022.12.09 |
---|---|
LRU(Least Recently Used) (0) | 2022.12.06 |
삽입정렬 (0) | 2022.12.03 |
선택정렬 (0) | 2022.12.02 |
후위연산 (0) | 2022.12.01 |