DEV Community

Cover image for Mastering DSA: 5 Beginner-Friendly Algorithms with Examples
Abdul Salam
Abdul Salam

Posted on

Mastering DSA: 5 Beginner-Friendly Algorithms with Examples

Top 5 DSA Algorithms Every Beginner Should Learn (With Examples)

Starting your journey in Data Structures and Algorithms (DSA) can feel overwhelming. To make it simpler, here are the top 5 beginner-friendly algorithms that will build your foundation in DSA. We'll explain each in a simple, easy-to-follow way with practical examples.

1. Sorting Algorithms

Sorting is all about arranging data in a specific order, like ascending or descending. Let’s start with two basic sorting methods.

Bubble Sort
Imagine bubbles rising in water. The largest bubble moves to the top in every step. Similarly, Bubble Sort compares adjacent numbers, swaps them if they’re out of order, and repeats until the list is sorted.

Example:

Sort [4, 3, 2, 1] into ascending order.

  • 1st Pass: [3, 4, 2, 1] → [3, 2, 4, 1] → [3, 2, 1, 4]
  • 2nd Pass: [2, 3, 1, 4] → [2, 1, 3, 4]
  • 3rd Pass: [1, 2, 3, 4]

Python Code:

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

print(bubble_sort([4, 3, 2, 1]))
Enter fullscreen mode Exit fullscreen mode

Selection Sort:

This algorithm picks the smallest number and moves it to the front. It repeats the process for the unsorted part of the list.

Example:

Sort [4, 3, 2, 1].
1st Step: [1, 3, 2, 4]
2nd Step: [1, 2, 3, 4]

Python Code:

def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i+1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return arr

print(selection_sort([4, 3, 2, 1]))
Enter fullscreen mode Exit fullscreen mode

2. Searching Algorithms

Linear Search:

Linear Search looks for a specific number by checking each item one by one.

Example:

Find 3 in [4, 3, 2, 1].
Check: 4 → 3 (Found!).

Python Code:

def linear_search(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i
    return -1

print(linear_search([4, 3, 2, 1], 3))
Enter fullscreen mode Exit fullscreen mode

Binary Search:

Binary Search divides the sorted list into halves to find the target.

Example:

Find 3 in [1, 2, 3, 4].

  1. Middle number: 3 (Found!)

Python Code:

def binary_search(arr, target):
    low, high = 0, len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

print(binary_search([1, 2, 3, 4], 3))
Enter fullscreen mode Exit fullscreen mode

3. Recursion

Recursion is when a function calls itself to solve smaller problems.

Factorial Example

Factorial of n (n!) is the product of all numbers from n down to 1.

Example:

4! = 4 * 3 * 2 * 1 = 24

Python Code:

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)

print(factorial(4))
Enter fullscreen mode Exit fullscreen mode

4. Greedy Algorithms

Activity Selection Problem

The goal is to select the maximum number of activities that don’t overlap.

Steps:

  1. Sort activities by their ending times.
  2. Pick the first activity.
  3. Skip overlapping ones and pick the next non-overlapping activity.

Example:

Activities: [(1, 2), (3, 4), (0, 6), (5, 7)]
Selected: (1, 2), (3, 4), (5, 7)

Python Code:

def activity_selection(activities):
    activities.sort(key=lambda x: x[1])
    selected = [activities[0]]
    for i in range(1, len(activities)):
        if activities[i][0] >= selected[-1][1]:
            selected.append(activities[i])
    return selected

print(activity_selection([(1, 2), (3, 4), (0, 6), (5, 7)]))
Enter fullscreen mode Exit fullscreen mode

5. Dynamic Programming (DP)

Fibonacci Series (DP Approach)

Fibonacci numbers follow this pattern: Each number is the sum of the previous two.

Example:

For n = 5, Fibonacci sequence is 0, 1, 1, 2, 3, 5.

Python Code:

def fibonacci(n):
    dp = [0, 1]
    for i in range(2, n+1):
        dp.append(dp[i-1] + dp[i-2])
    return dp[n]

print(fibonacci(5))
Enter fullscreen mode Exit fullscreen mode

Mastering these five algorithms will give you a solid understanding of basic DSA concepts. Start by practicing each step, understand how they work, and implement them yourself. With time, you’ll gain the confidence to tackle advanced algorithms and complex problems. Happy coding!

Top comments (0)