Algorithm
Algorithm
A step-by-step procedure for solving a problem. Efficiency is measured by time/space complexity. Sorting and searching are the basics.
Algorithm
A step-by-step procedure for solving a problem. Efficiency is measured by time/space complexity. Sorting and searching are the basics.
An algorithm is a clear and finite step-by-step procedure for solving a specific problem. Its etymology derives from the Latin name of the 9th-century Persian mathematician Al-Khwarizmi, and as a fundamental concept of computer science, it forms the core logic of a program.
The efficiency of an algorithm is measured by time complexity and space complexity. Big-O notation is used to express how performance changes with input size, where O(1) means constant time, O(n) linear time, O(log n) logarithmic time, and O(n^2) quadratic time complexity.
Representative types of algorithms include sorting, searching, graph traversal, dynamic programming, and divide and conquer. Each type is optimized for a specific problem pattern, and choosing the appropriate algorithm determines system performance.
In practice, algorithms are applied everywhere, including database indexing, network routing, recommendation systems, and search engine ranking. They are also a key evaluation element in coding tests and technical interviews, and as of 2025, in the AI/ML field, neural network optimization algorithms (such as SGD and Adam) are used as the core of model training.
# 1. 퀵 정렬 (Quick Sort) - O(n log n) 평균
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
numbers = [64, 34, 25, 12, 22, 11, 90]
print(f"정렬 전: {numbers}")
print(f"정렬 후: {quick_sort(numbers)}") # [11, 12, 22, 25, 34, 64, 90]
# 2. 이진 탐색 (Binary Search) - O(log n)
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
sorted_arr = [11, 12, 22, 25, 34, 64, 90]
index = binary_search(sorted_arr, 25)
print(f"25의 인덱스: {index}") # 3
# 3. 동적 프로그래밍 - 피보나치 (메모이제이션)
from functools import lru_cache
@lru_cache(maxsize=None)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(f"피보나치(50): {fibonacci(50)}") # 12586269025
# 4. BFS (너비 우선 탐색) - 그래프 최단 경로
from collections import deque
def bfs(graph, start):
visited = set([start])
queue = deque([start])
result = []
while queue:
node = queue.popleft()
result.append(node)
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return result
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E']
}
print(f"BFS 탐색 순서: {bfs(graph, 'A')}") # ['A', 'B', 'C', 'D', 'E', 'F']
"Our current search function is O(n), so it will become a bottleneck when the data grows to 1 million records. I think we should consider either adding an index to optimize it to O(log n), or introducing a caching layer for frequently queried results."
"The average time complexity of quicksort is O(n log n), but if you choose the first element as the pivot on an already sorted array, it degrades to O(n^2). To prevent this, we use a random pivot or the median-of-three method."
"This double loop is O(n^2). Using a hash map can reduce it to O(n). It's not a problem at the current data scale, but considering scalability, it would be good to improve it now."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
"Premature optimization is the root of all evil" - First write code that works correctly, then identify bottlenecks through profiling, and only then optimize. Optimizing something that is not an actual bottleneck is a waste of time.
Looking only at average complexity is risky. Quicksort is O(n log n) on average, but O(n^2) in the worst case. In real-time systems, you need to choose an algorithm that also takes the worst case into account.
To save time, you sacrifice space (caching, memoization), and to save space, it takes more time. Find a balance that suits the situation. If memory is sufficient, aim for O(1) lookups with a hash table.