Q1. 버블 정렬의 시간 복잡도는 무엇인가?
- O(N2)이다.
Q2. 자바로 버블 정렬을 직접 구현해봐라
public int[] bubbleSort(int[] arr){
int n = arr.length;
for(int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j] > arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
return arr;
}
참고
Q1~Q2 누구나 자료구조와 알고리즘 6/23