Skip to content

Commit 6d489cb

Browse files
committed
feat: 삽입 정렬 구현
1 parent 0627441 commit 6d489cb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sort;
2+
3+
public class InsertionSort implements Sort {
4+
5+
@Override
6+
public void sort(int[] array) {
7+
int length = array.length;
8+
int insertData;
9+
10+
for (int i = 1; i < length; i++) {
11+
insertData = array[i];
12+
int j;
13+
14+
for (j = i - 1; j >= 0; j--) { // 정렬된 배열의 뒷부분 부터 비교해서 삽입위치 탐색
15+
if (array[j] > insertData) { // 삽입할 데이터가 작으면
16+
array[j + 1] = array[j]; // 위치 변경
17+
} else {
18+
break; // 해당 위치에 삽입되어야 하므로 탈출
19+
}
20+
}
21+
22+
array[j + 1] = insertData;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)