forked from jbee37142/algorithm_basic_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
45 lines (38 loc) · 1.04 KB
/
InsertionSort.java
File metadata and controls
45 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package sort;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class InsertionSort {
/*
TASK
Insertion sort를 구현한다.
*/
@Test
public void test() {
int[] arr1 = {};
int[] sortedArr1 = {};
assertThat(solution(arr1), is(sortedArr1));
int[] arr2 = {6,4,1,8,9,2,7,5,3};
int[] sortedArr2 = {1,2,3,4,5,6,7,8,9};
assertThat(solution(arr2), is(sortedArr2));
int[] arr3 = {1};
int[] sortedArr3 = {1};
assertThat(solution(arr3), is(sortedArr3));
}
public int[] solution(int[] arr) {
if (arr == null) return null;
int temp;
for (int i = 1; i < arr.length; i++) {
temp = arr[i];
int k;
for (k = i - 1; k >= 0; k--) {
if (temp >= arr[k]) {
break;
}
arr[k + 1] = arr[k];
}
arr[k + 1] = temp;
}
return arr;
}
}