Sobes.tech
Junior — Middle

Can you name the main classical algorithms you know?

sobes.tech AI

Answer from AI

Main classical algorithms that are often studied and used:

  • Sorting: bubble sort, quicksort, merge sort, insertion sort.
  • Searching: binary search, linear search.
  • Graph algorithms: depth-first search (DFS), breadth-first search (BFS), Dijkstra's algorithm for shortest path, Prim's and Kruskal's algorithms for minimum spanning tree.
  • String algorithms: substring search (e.g., Knuth-Morris-Pratt algorithm).
  • Dynamic programming: solving optimization problems, such as the knapsack problem, Fibonacci number calculation.

Example of simple binary search in Python:

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