Data Structures & Algorithms

Data Structures & Algorithms

In the world of computers, data structures and algorithms are the essential tools that software engineers rely on. They're like the blueprint and instructions for building amazing digital creations. These tools help us organize and process information effectively, making our software work smoothly and efficiently. Join us as we dive into the world of data structures and algorithms, where we'll unravel their secrets and explore how they shape the technology we use every day.

Understanding Data Structures and Algorithms

Data Structures:

At its core, a data structure is a way of organizing and storing data in a computer so that it can be accessed and manipulated efficiently. Think of it as a blueprint that dictates how data will be organized, stored, and accessed within a program. Data structures range from simple arrays and linked lists to more complex structures like trees, graphs, and hash tables.

Algorithms:

An algorithm is a set of step-by-step instructions designed to perform a specific task or solve a particular problem. It is the computational counterpart to a recipe, guiding the computer through a sequence of operations to achieve a desired outcome. Algorithms can range from simple operations like sorting and searching to complex computational tasks like machine learning and cryptography.

Sorting Algorithms

Sorting algorithms are fundamental operations in computer science, essential for arranging elements in a specific order. Let's explore two popular sorting algorithms,

  • Bubble Sort
  • Selection Sort



Bubble Sort

Bubble Sort is a straightforward algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the entire list is sorted.

How it works:

  • Pass through the list, compare the values of each adjacent pair in the list
  • For each adjacent pair, swap the values if they are in the wrong order.
  • Repeat 1 and 2 until no swaps are made in a pass.

Article content
Step 1
Article content
Step 2
Article content
Step 3
#python

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]

# Example usage

arr = [33, 91, 76, 8, 22]
bubble_sort(arr)
print("Sorted array:", arr)

        

Advantages of Bubble Sort:

  • Bubble sort is easy to understand and implement.
  • It does not require any additional memory space.
  • It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the sorted output.

Disadvantages of Bubble Sort:

  • Bubble sort has a time complexity of O(N**2) which makes it very slow for large data sets.
  • Bubble sort is a comparison-based sorting algorithm, which means that it requires a comparison operator to determine the relative order of elements in the input data set. It can limit the efficiency of the algorithm in certain cases.



Selection Sort

Article content
Selection Sort Algorithm

Selection Sort divides the list into two parts: a sorted subarray and an unsorted subarray. It repeatedly selects the smallest (or largest) element from the unsorted subarray and moves it to the beginning of the sorted subarray.

#python

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]


#Example usage

arr = [64, 34, 25, 12, 22, 11, 90]
selection_sort(arr)
print("Sorted array:", arr)        



Searching Algorithms

Searching algorithms are essential for locating specific elements within a collection of data. Let's examine two common searching algorithms:

  • Linear Searching
  • Binary Searching

Linear Searching

Linear Search sequentially checks each element in the list until the target element is found or the entire list has been traversed.

#python

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



# Example usage

arr = [64, 34, 25, 12, 22, 11, 90]
target = 22
index = linear_search(arr, target)
print("Element", target, "found at index:", index)        

Binary Searching

Article content
Linear Searching Algorithm

Binary Search operates on sorted arrays and repeatedly divides the search interval in half until the target element is found.

How it works:

  • Divide the search space into two halves by finding the middle index "mid".

Article content

  • Compare the middle element of the search space with the key. 
  • If the key is found at middle element, the process is terminated.
  • If the key is not found at middle element, choose which half will be used as the next search space.
  • If the key is smaller than the middle element, then the left side is used for next search.
  • If the key is larger than the middle element, then the right side is used for next search.
  • This process is continued until the key is found or the total search space is exhausted.

#python

def binary_search(arr, target):
    low = 0
    high = 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



# Example usage (requires sorted array)

arr = [11, 12, 22, 25, 34, 64, 90]
target = 22
index = binary_search(arr, target)
print("Element", target, "found at index:", index)

        

Arrays, Queues, Lists, Linked Lists, and Objects

  • Arrays Arrays are contiguous blocks of memory used to store elements of the same data type.
  • Queues Queues are linear data structures that follow the First-In-First-Out (FIFO) principle, commonly used for task scheduling and job processing.
  • Lists Lists are collections of elements that can dynamically grow or shrink in size, offering flexibility in data storage.
  • Linked Lists

Article content
Linked List Data Structure

Linked Lists consist of nodes linked together by pointers, enabling efficient insertion and deletion operations.

  • Objects Objects encapsulate data and behavior into a single entity, facilitating modular and reusable code organization.
  • Big O notation A mathematical notation that is used to describe the efficiency of an algorithm. It expresses the time complexity of an algorithm, which is the amount of time it takes to run an algorithm as a function of the size of the input data. Big O notation uses letters such as O, N, log N, etc., to represent the time complexity of an algorithm.

Understanding Complexity

Complexity refers to the performance characteristics of algorithms concerning their time and space requirements.

  • Time Complexity: Time complexity measures the amount of time an algorithm takes to complete its execution, often expressed in Big O notation.
  • Space Complexity: Space complexity quantifies the amount of memory an algorithm requires to solve a problem.

Finding Complexity

Analyzing the complexity of an algorithm involves assessing its behavior concerning input size and identifying dominant operations.

Binary Search:

  • Time Complexity: O(log n)
  • Explanation: Binary Search operates on sorted arrays and repeatedly divides the search space in half until the target element is found. Since it halves the search space in each iteration, it has a logarithmic time complexity.
  • Example: If you have an array of size n, Binary Search can find an element in approximately log₂(n) iterations.

Bubble Sort:

  • Time Complexity: O(n^2)
  • Explanation: Bubble Sort compares adjacent elements and swaps them if they are in the wrong order, repeatedly going through the list until it's sorted. In the worst case, it needs to do n comparisons for each of the n elements, resulting in a quadratic time complexity.
  • Example: If you have an array of size n, Bubble Sort may need to perform approximately n²/2 comparisons in the worst case.


Practical Application and Practice

To reinforce your understanding and improve your skills in data structures and algorithms, consider practicing on platforms like LeetCode. LeetCode offers a plethora of coding problems categorized by difficulty and topic, allowing you to apply your knowledge in a real-world context.

Article content
LeetCode


To view or add a comment, sign in

More articles by Ashan Wijerathna

Insights from the community

Others also viewed

Explore topics