Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2ceb698

Browse filesBrowse files
committed
Add sort code
1 parent b52665a commit 2ceb698
Copy full SHA for 2ceb698

2 files changed

+91Lines changed: 91 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/test/java/sort/BubbleSort.java‎

Copy file name to clipboard
+43Lines 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+
}
Collapse file

‎src/test/java/sort/RadixSort.java‎

Copy file name to clipboard
+48Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.