Skip to content

Commit 2ceb698

Browse files
committed
Add sort code
1 parent b52665a commit 2ceb698

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/test/java/sort/BubbleSort.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sort;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class BubbleSort {
9+
10+
/*
11+
TASK
12+
bubble sort를 구현한다.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
int[] arr = new int[5];
18+
arr[0] = 2;
19+
arr[1] = 1;
20+
arr[2] = 4;
21+
arr[3] = 0;
22+
arr[4] = 3;
23+
24+
int[] sortedArr = new int[arr.length];
25+
for (int i = 0; i < sortedArr.length; i++) {
26+
sortedArr[i] = i;
27+
}
28+
assertThat(sort(arr), is(sortedArr));
29+
}
30+
31+
public int[] sort(int[] arr) {
32+
for (int i = 0; i < arr.length - 1; i++) {
33+
for (int j = i + 1; j < arr.length; j++) {
34+
if (arr[i] > arr[j]) {
35+
int temp = arr[i];
36+
arr[i] = arr[j];
37+
arr[j] = temp;
38+
}
39+
}
40+
}
41+
return arr;
42+
}
43+
}

src/test/java/sort/RadixSort.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sort;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class RadixSort {
9+
10+
/*
11+
TASK
12+
radix sort를 구현한다.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
int[] arr = new int[5];
18+
arr[0] = 52;
19+
arr[1] = 31;
20+
arr[2] = 24;
21+
arr[3] = 45;
22+
arr[4] = 13;
23+
24+
int[] sortedArr = new int[arr.length];
25+
sortedArr[0] = 13;
26+
sortedArr[1] = 24;
27+
sortedArr[2] = 31;
28+
sortedArr[3] = 45;
29+
sortedArr[4] = 52;
30+
assertThat(sort(arr), is(sortedArr));
31+
}
32+
33+
public int[] sort(int[] arr) {
34+
int[] newArr = new int[100];
35+
int[] result = new int[arr.length];
36+
for (int item : arr) {
37+
newArr[item] = item;
38+
}
39+
int index = 0;
40+
for (int item : newArr) {
41+
if (item != 0) {
42+
result[index++] = item;
43+
}
44+
}
45+
return result;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)