Data Structures and Algorithms Code Snippets

Data Structures and Algorithms Implementations

Merge Sort Implementation

The mergeSort method recursively divides the array and then merges the sorted halves.

public static void mergeSort(int[] array) {
    int length = array.length;
    if (length <= 1)
        return;

    int middle = length / 2;
    int[] leftArray = new int[middle];
    int[] rightArray = new int[length - middle];

    int j = 0;
    for (int i = 0; i < length; i++) {
        if (i < middle)
            leftArray[i] =
Read More

Data Structure Algorithms: Linked Lists, Trees, and Hashing

SINGLY LINKED LIST

// Insert at beginning 0

Algorithm InsertAtBeginning(data)
:1. NewNode = (Node*)malloc(sizeof(Node))2. NewNode->data = data3. NewNode->next = head4. head = newNode

// Insert at endAlgorithm InsertAtEnd(data):1. NewNode = (Node*)malloc(sizeof(Node))2. NewNode->data = data3. NewNode->next = NULL4. If head == NULL:

head = newNodeElse:temp = headWhile temp->next != NULL:temp = temp->nexttemp->next = newNode;

// Insert at position Algorithm InsertAtPos(data, pos):1.

Read More

Essential C Programming Examples: Data Structures & Algorithms

Fundamental C Operations (String and Math)

String Reversal Without Built-in Functions

This program demonstrates manual string reversal using pointers and loops, calculating the string length without relying on strlen().

#include <stdio.h>
#include <string.h>
int main() {
    char str[100], original[100];
    char *start, *end, temp;
    int length = 0;
    printf("Enter a string: ");
    scanf("%s", str); // accepts string without spaces
    strcpy(original, str); // keep a copy of original 
Read More

Power BI Data Modeling: Star Schema, Normalization & KPIs

Flat Table

A Flat Table stores all data in a single table. It is simple and quick to build, making it suitable for very small or basic analyses. However, it leads to duplicated data, which increases the risk of errors and inconsistencies. As the dataset grows, performance and scalability worsen. For this reason, flat tables are not recommended for advanced or professional Power BI reports.


Relational Model

A Relational Model organizes data into multiple tables connected through keys. This structure

Read More

Data Structures and Algorithms Exam Review

1) Recurrences

1.1 10-mark: Solve T(n)=2T(n/2)+n, T(1)=c

T(n)=2T(n/2)+n
=4T(n/4)+2n
=8T(n/8)+3n
… after i steps: T(n)=2i T(n/2i) + i*n
Stop: n/2i=1 => i=log2 n
T(n)=n*T(1) + n log2 n = n*c + n log2 n
=> T(n)=Θ(n log n)

1.2 Master Theorem (Write As-Is)

T(n)=aT(n/b)+f(n), compare f(n) with nlogb a

  • Case 1: f(n)=O(nlogb a – ε) => T(n)=Θ(nlogb a)
  • Case 2: f(n)=Θ(nlogb a logk n) => T(n)=Θ(nlogb a logk+1 n)
  • Case 3: f(n)=Ω(nlogb a + ε) and a f(n/b) ≤ c f(n) (c<1) => T(n)=Θ(f(n))

2)

Read More

Essential Concepts in Statistics, Machine Learning, and Network Security

Statistical Foundations and Predictive Modeling


Understanding the Central Limit Theorem (CLT)

The Central Limit Theorem (CLT) is one of the most important principles in statistics. It states that when we take many random samples from any population—regardless of the population’s original distribution—the distribution of the sample means will approach a normal (bell-shaped) distribution as the sample size becomes large enough (usually n ≥ 30).

This phenomenon occurs even if the population itself

Read More