1. Home
  2. Explore
  3. Computer Science
  4. Key Computer Science Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Key Computer Science Algorithms

Speaker 1Speaker 2

Key Computer Science Algorithms

6:02
0:00-6:02
  1. Corey

    Welcome back! Today we're tackling six essential algorithm types that show up in computer science courses and interviews. We'll cover recursion, searching, sorting, pathfinding, minimum spanning trees, and dynamic programming.

  2. Maria

    That's a lot! But I've heard these terms before—recursion especially seems tricky. Let's start there. What exactly is recursion?

  3. Corey

    At its simplest, recursion is when a function calls itself. To design a recursive algorithm, you need two things: a base case that gives you an answer directly, and a recursive case that breaks the problem into smaller subproblems. The classic example is the Fibonacci sequence: each term is the sum of the two previous ones.

  4. Maria

    So for Fibonacci, the base cases would be the first two terms, both equal to 1. Then the recursive case is F(n) = F(n-1) + F(n-2). That makes sense. But I've heard that plain recursion can be really slow.

  5. Corey

    Exactly. That naive recursive Fibonacci has a time complexity of O(2^n) because it recalculates the same values over and over. For n=70, it would take forever. The fix is memoization: caching results so you compute each term only once. That drops it to O(n) time, though it uses O(n) space. You can even go further and use an iterative approach with just two variables, getting O(n) time and O(1) space.

  6. Maria

    Got it. So recursion is about breaking down problems, and memoization is a way to cache subproblem results. Now let's talk about searching. I know linear search is just checking each element one by one—O(n). But binary search sounds way faster.

  7. Corey

    Yes, binary search only works on sorted data. You look at the middle element, compare it to your target, and then discard half the array based on that comparison. Repeat until you find it or the subarray is empty. That gives you O(log n) time. It's incredibly efficient for large datasets.

  8. Maria

    But what if the data isn't sorted? Then you'd need to sort it first, which is O(n log n). So sometimes a linear scan is better if you only search once.

  9. Corey

    Exactly. Now, moving to more complex traversals: depth-first search and breadth-first search. These are used for graphs and mazes. In DFS, you go as deep as possible along one path before backtracking. In BFS, you explore all neighbors at the current depth first.

  10. Maria

    I've seen this with a maze example. DFS acts like a snake, going one direction until it hits a wall, then backtracking. BFS expands outward like a wave, visiting nodes level by level.

  11. Corey

    Right. In the maze, DFS might find the exit quickly if the path is straight, but BFS guarantees the shortest path in an unweighted graph. Both can be implemented with a stack (DFS) or a queue (BFS). Time complexity is O(V+E) with a proper visited set.

  12. Maria

    Alright, now sorting. We studied selection, insertion, and bubble sort as the simple ones. They're all O(n^2). Selection sort repeatedly picks the smallest element and swaps it to the front. Insertion sort builds a sorted portion by inserting each new element in the right place. Bubble sort swaps adjacent elements to bubble the largest to the end.

  13. Corey

    And then we have the efficient ones: merge sort, heap sort, and quicksort, all O(n log n). Merge sort is a recursive divide-and-conquer: split the array in half, sort each half recursively, then merge the sorted halves. The merge step uses two pointers and takes O(n) per level.

  14. Maria

    So the recursion for merge sort goes until we have single-element arrays, which are trivially sorted. Then we merge back up. That's why it's stable and guaranteed O(n log n). Heap sort is different—it builds a heap from the array, then repeatedly extracts the max (or min) to build the sorted list. It's not stable but also O(n log n).

  15. Corey

    And quicksort is usually the fastest in practice, though it has a worst-case O(n^2) if the pivot is poorly chosen. It picks a pivot, partitions the array so all smaller elements go left and larger go right, and then recursively sorts the partitions. Most languages use quicksort or a variant as their default sort.

  16. Maria

    Now for pathfinding: Dijkstra's algorithm finds the shortest path from a start node to all other nodes in a weighted graph. It uses a priority queue (min-heap) to always expand the node with the smallest known distance, updating distances to its neighbors. The visited set prevents revisiting nodes.

  17. Corey

    Exactly. The time complexity is O((V+E) log V) with a heap. But if you only need the shortest path to one target and the graph is grid-like, you can use A* which adds a heuristic (like straight-line distance) to prioritize nodes that are closer to the goal. That prunes the search significantly.

  18. Maria

    Next, minimum spanning trees. Kruskal's algorithm is straightforward: sort all edges by weight, then add them one by one to the tree, skipping those that would create a cycle. You stop when you have V-1 edges. The cycle detection is done with a union-find data structure.

  19. Corey

    Right. The time complexity is dominated by sorting: O(E log E). For a connected graph, E is at least V-1, so we can simplify to O(E log V). There's also Prim's algorithm, but Kruskal is the one we covered.

  20. Maria

    Finally, dynamic programming. We used the minimum sum of a continuous subarray problem. The naive approach checks all subarrays—O(n^3) if you're not careful. But with DP, we store the minimum sum ending at each index: at each step, we either start a new subarray with the current element, or extend the previous best subarray. That gives O(n) time and O(1) space.

  21. Corey

    Exactly. DP is about breaking a problem into overlapping subproblems and using their solutions to build up. That's how we went from an exponential or cubic algorithm to linear. It's a powerful technique for optimization problems.

  22. Maria

    This has been a great overview. We covered recursion, searching, sorting, pathfinding, MSTs, and DP—all with practical examples. Thanks for walking through it all!

  23. Corey

    You're welcome! Remember, understanding the concepts is more important than memorizing code. Keep practicing, and these algorithms will become second nature.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent