public class BubbleSort {
public static void main(String[] args) {
int temp = 0;
int arr[] = {2, 3, 5, 1, 4};
System.out.println("정렬 전 배열 값 출력");
for(int i=0 ; i<arr.length ; i++)
System.out.print(arr[i] + " ");
for(int i=0 ; i<arr.length-1 ; i++){
for(int j=0 ; j<(arr.length-1)-i ; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println();
System.out.println("정렬 후 배열 값 출력");
for(int i=0 ; i<arr.length ; i++)
System.out.print(arr[i] + " ");
}
}
- 결과