Skip to content

Commit 58c2547

Browse files
committed
Add search code
1 parent 19670fc commit 58c2547

File tree

2 files changed

+147
-3
lines changed

2 files changed

+147
-3
lines changed
Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,69 @@
11
package search;
22

3-
/**
4-
* Created by Jbee on 2017. 6. 1..
5-
*/
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
68
public class BinarySearchTest {
9+
10+
/*
11+
TASK
12+
binary search를 사용하여 O(log n)의 시간복잡도로 target을 찾는다.
13+
*/
14+
@Test
15+
public void test() {
16+
int[] arr = new int[7];
17+
arr[0] = 52;
18+
arr[1] = 31;
19+
arr[2] = 24;
20+
arr[3] = 45;
21+
arr[4] = 13;
22+
arr[5] = 11;
23+
arr[6] = 28;
24+
assertThat(searchByRec(arr, 24), is(2));
25+
assertThat(search(arr, 24), is(2));
26+
}
27+
28+
// while version
29+
private int search(int[] arr, int target) {
30+
if (arr == null) return -1;
31+
int left = 0;
32+
int right = arr.length - 1;
33+
34+
while (left <= right) {
35+
int mid = left + (right - left) / 2;
36+
if (arr[mid] == target) {
37+
return mid;
38+
}
39+
40+
if (arr[mid] < target) {
41+
left = mid;
42+
right -= 1;
43+
} else {
44+
right = mid;
45+
left += 1;
46+
}
47+
}
48+
return -1;
49+
}
50+
51+
//recursive version
52+
private int searchByRec(int[] arr, int target) {
53+
if (arr == null) return -1;
54+
return searchRec(arr, 0, arr.length - 1, target);
55+
}
56+
57+
private int searchRec(int[] arr, int left, int right, int target) {
58+
if (left > right || left < 0 || right >= arr.length) return -1;
59+
int mid = left + (right - left) / 2;
60+
61+
if (arr[mid] == target) {
62+
return mid;
63+
} else if (arr[mid] < target) {
64+
return searchRec(arr, mid, right - 1, target);
65+
} else {
66+
return searchRec(arr, left + 1, mid, target);
67+
}
68+
}
769
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package search;
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 SearchIn2DTest {
9+
10+
/*
11+
TASK
12+
정렬된 2차원 배열에서 검색한다.
13+
1. 각 row 별로 for-loop 돌면서 O(log n)의 sort을 한다.
14+
2. O(n)
15+
*/
16+
17+
@Test
18+
public void test() {
19+
int[][] matrix = new int[5][5];
20+
for (int i = 0; i < matrix[0].length; i++) {
21+
for (int j = 0; j < matrix[0].length; j++) {
22+
matrix[i][j] = i + j;
23+
}
24+
}
25+
assertThat(getTargetPosition(matrix, 5), is(new Position(4,1)));
26+
}
27+
28+
public Position getTargetPosition(int[][] matrix, int target) {
29+
if (matrix == null) return null;
30+
31+
int row = matrix.length - 1;
32+
int col = 0;
33+
34+
while (row >= 0 && col < matrix[0].length) {
35+
if (matrix[row][col] == target) {
36+
return new Position(row, col);
37+
} else if (matrix[row][col] < target) {
38+
col++;
39+
} else {
40+
row--;
41+
}
42+
}
43+
44+
return null;
45+
}
46+
47+
public class Position {
48+
int row;
49+
int col;
50+
51+
public Position(int row, int col) {
52+
this.row = row;
53+
this.col = col;
54+
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) return true;
59+
if (o == null || getClass() != o.getClass()) return false;
60+
61+
Position position = (Position) o;
62+
63+
if (row != position.row) return false;
64+
return col == position.col;
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
int result = row;
70+
result = 31 * result + col;
71+
return result;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return "Position{" +
77+
"row=" + row +
78+
", col=" + col +
79+
'}';
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)