contents
π μ½λ© ν μ€νΈλ₯Ό μν νμ μκ³ λ¦¬μ¦ κ°μ΄λ
μ½λ© ν μ€νΈμ κΈ°μ λ©΄μ μμ μμ£Ό λ±μ₯νλ νμ μκ³ λ¦¬μ¦μ μμΈν μ 리νμ΅λλ€. κ° μκ³ λ¦¬μ¦μ ν΅μ¬ κ°λ , μ¬μ© μ¬λ‘, μκ° λ³΅μ‘λ, Java μμ μ½λλ₯Ό ν¬ν¨ν©λλ€.
1. π μ ν νμ (Linear Search)
π§ κ°μ:
λ°°μ΄μ 첫 λ²μ§Έ μμλΆν° μμ°¨μ μΌλ‘ νμνλ©° μνλ κ°μ μ°Ύμ΅λλ€.
β± μκ° λ³΅μ‘λ: O(n)
π§© μ¬μ© μ: μ λ ¬λμ§ μμ μμ λ°°μ΄
int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++)
if (arr[i] == target) return i;
return -1;
}
2. π μ΄μ§ νμ (Binary Search)
π§ κ°μ:
μ λ ¬λ λ°°μ΄μ λν΄, κ°μ΄λ° κ°μ κΈ°μ€μΌλ‘ νμ λ²μλ₯Ό λ°μΌλ‘ μ€μ΄λ©° κ²μν©λλ€.
β± μκ° λ³΅μ‘λ: O(log n)
π§© μ¬μ© μ: μ λ ¬λ λ°°μ΄μ κ° κ²μ
int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
β μμ©:
- μ€λ³΅κ° μ€ μ²«/λ§μ§λ§ μμΉ μ°ΎκΈ°
- lower_bound, upper_bound ꡬν λ±
3. π μΌλΆ νμ (Ternary Search)
π§ κ°μ:
λ°μ΄ν°κ° "λμλ€ μμμ§λ" (unimodal) 곑μ (ν¨μ)μΌ λ, μ΅λ/μ΅μκ°μ λΉ λ₯΄κ² μ°ΎκΈ° μν λ‘κ·Έ κΈ νμ
β± μκ° λ³΅μ‘λ: O(log n)
π§© μ¬μ© μ: μ΅λκ° μμΉ μ°ΎκΈ° (ex: μ΄μ§ νμ λμ μΈ μ μμ)
double ternarySearch(double left, double right, Function<Double, Double> f) {
double eps = 1e-9;
while (right - left > eps) {
double m1 = left + (right - left) / 3;
double m2 = right - (right - left) / 3;
if (f.apply(m1) < f.apply(m2)) left = m1;
else right = m2;
}
return (left + right) / 2;
}
4. π μ ν νμ (Jump Search)
π§ κ°μ:
μ λ ¬λ λ°°μ΄μμ μΌμ κ°κ²©(βn)λ§νΌ 건λλ°λ©° νμ. λ²μ λ΄μ λ€μ΄κ° λ€ μ ν νμμ μν.
β± μκ° λ³΅μ‘λ: O(βn)
π§© μ¬μ© μ: μ λ ¬λ μ€κ° ν¬κΈ° 리μ€νΈ
int jumpSearch(int[] arr, int x) {
int n = arr.length, step = (int)Math.floor(Math.sqrt(n));
int prev = 0;
while (arr[Math.min(step, n) - 1] < x) {
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n) return -1;
}
for (int i = prev; i < Math.min(step, n); i++)
if (arr[i] == x) return i;
return -1;
}
5. π μ§μ νμ (Exponential Search)
π§ κ°μ:
λ¨Όμ 1, 2, 4, 8 μμΌλ‘ λ²μλ₯Ό λνλ©° μ μ ν ꡬκ°μ μ°Ύκ³ , κ·Έ μμμ μ΄μ§ νμ μν.
β± μκ° λ³΅μ‘λ: O(log n)
int exponentialSearch(int[] arr, int x) {
int n = arr.length, bound = 1;
while (bound < n && arr[bound] < x) bound *= 2;
int left = bound / 2, right = Math.min(bound, n - 1);
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
6. π λ³΄κ° νμ (Interpolation Search)
π§ κ°μ:
μ λ ¬λ κ°μ΄ κ· λ±νκ² λΆν¬λμ΄ μμμ κ°μ νκ³ , μ΄μ§ νμλ³΄λ€ λ μ κ΅νκ² μμΉλ₯Ό μΆμΈ‘.
β± μκ° λ³΅μ‘λ: νκ· O(log log n), μ΅μ
O(n)
π§© λ¨μ : λ°°μ΄μ΄ κ· μΌνμ§ μμΌλ©΄ λΉν¨μ¨μ
int interpolationSearch(int arr[], int x) {
int lo = 0, hi = arr.length - 1;
while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
int pos = lo + ((hi-lo)*(x-arr[lo])) / (arr[hi]-arr[lo]);
if (arr[pos] == x) return pos;
if (arr[pos] < x) lo = pos + 1;
else hi = pos - 1;
}
return -1;
}
7. π νΌλ³΄λμΉ νμ (Fibonacci Search)
π§ κ°μ:
νΌλ³΄λμΉ μμ΄μ κΈ°λ°μΌλ‘ μΌμͺ½/μ€λ₯Έμͺ½ λ²μ λΆν . μ΄μ§ νμμ λμ
β± μκ° λ³΅μ‘λ: O(log n)
int fibonacciSearch(int arr[], int x) {
int n = arr.length;
int fibMm2 = 0, fibMm1 = 1, fibM = fibMm1 + fibMm2;
while (fibM < n) {
fibMm2 = fibMm1;
fibMm1 = fibM;
fibM = fibMm1 + fibMm2;
}
int offset = -1;
while (fibM > 1) {
int i = Math.min(offset + fibMm2, n - 1);
if (arr[i] < x) {
fibM = fibMm1;
fibMm1 = fibMm2;
fibMm2 = fibM - fibMm1;
offset = i;
} else if (arr[i] > x) {
fibM = fibMm2;
fibMm1 -= fibMm2;
fibMm2 = fibM - fibMm1;
} else return i;
}
if (fibMm1 == 1 && arr[offset + 1] == x) return offset + 1;
return -1;
}
8. π μλ£κ΅¬μ‘° κΈ°λ° νμ
πΈ ν΄μ νμ (Hash Table)
- νκ· : O(1)
- μ¬μ© μ: μ‘΄μ¬ μ¬λΆ νμΈ, μ€λ³΅ κ²μ¬ λ±
HashSet<Integer> set = new HashSet<>();
set.add(10);
boolean found = set.contains(10); // true
πΈ μ΄μ§ νμ νΈλ¦¬ (BST), κ· ν μ΄μ§ νΈλ¦¬
- νκ· : O(log n) (AVL, Red-Black trees λ± μ¬μ© μ)
- λ²μ κ²μ, floor/ceiling λ± μ λ ¬ κΈ°λ° μ°μ°μ μ 리
TreeSet<Integer> bst = new TreeSet<>();
bst.add(20); bst.add(5);
Integer next = bst.higher(10); // 20
9. π κ·Έλν/νΈλ¦¬ νμ β DFS & BFS
πΉ DFS (κΉμ΄ μ°μ νμ)
- κ°λ₯ν κΉμ΄κΉμ§ λ¨Όμ νμν λ€, λ°±νΈλνΉ
void dfs(int node, boolean[] visited, List<Integer>[] adj) {
visited[node] = true;
for (int next : adj[node])
if (!visited[next]) dfs(next, visited, adj);
}
πΉ BFS (λλΉ μ°μ νμ)
- κ°κΉμ΄ λ ΈλλΆν° λ 벨 μμΌλ‘ μν
void bfs(int start, boolean[] visited, List<Integer>[] adj) {
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
visited[start] = true;
while (!queue.isEmpty()) {
int node = queue.poll();
for (int next : adj[node]) {
if (!visited[next]) {
queue.add(next);
visited[next] = true;
}
}
}
}
10. π‘ μ€λ¬΄/ν μ€νΈ ν
- μμ/λΉμ λ ¬ μλ£ β Linear Search
- μ λ ¬λ μλ£ β Binary, Jump, Exponential
- μ μ νμ/μ€λ³΅ νμΈ β HashSet/Map
- κ·Έλν/νΈλ¦¬ νμ β BFS/DFS
- μ΅μκ°/μ΅λκ° μ°ΎκΈ° β Ternary Search
β μμ½ ν
| μκ³ λ¦¬μ¦ | μλ£κ΅¬μ‘°/쑰건 | μκ° λ³΅μ‘λ | νμ© μν© |
|---|---|---|---|
| μ ν νμ | μΌλ° λ°°μ΄ | O(n) | μ λ ¬ μ λ λ°μ΄ν° |
| μ΄μ§ νμ | μ λ ¬λ λ°°μ΄ | O(log n) | μ΄λΆ κ²μ, Lower/Upper Bound λ± |
| ν΄μ νμ | HashSet/Map | O(1) (νκ· ) | λΉ λ₯Έ μ‘΄μ¬ κ²μ¬, μ€λ³΅ μ κ±° |
| BST νμ | TreeSet/TreeMap | O(log n) | μ λ ¬ κΈ°λ° λ²μ κ²μ, floor/ceil |
| DFS/BFS | κ·Έλν/νΈλ¦¬ | O(N+E) | μ°κ²°μ± νμΈ, κ²½λ‘ νμ, μν 곡κ°μμ νμ |
references