Insertion Sort
동작 방식
1

2

3

구현
public class InsertionSort {
public static void main(String[] args) {
InsertionSort insertionSort = new InsertionSort();
int[] A = {5, 3, 4, 7, 2, 8, 6, 9, 1};
insertionSort.sort(A);
insertionSort.printArray(A);
}
public void sort(int[] A) {
for (int i = 1; i < A.length; i++) {
int key = A[i];
int target = i - 1;
while (target >= 0 && A[target] > key) {
A[target + 1] = A[target];
target--;
}
A[target + 1] = key;
}
}
public void printArray(int[] A) {
for (int num : A) {
System.out.print(num + " ");
}
}
}풀이
선택 가이드
시간 복잡도
장점
단점
Last updated