Homework Scrolls  ·  Chapter I

The Five Workings

Selected Studies in Craft & Logic

IBubble Sort IIBinary Conversion IIIGrade Calculator IVInsertion Sort VReverse Array
I

Bubble Sort

Comparison-Based Sorting  ·  O(N²)

Live Sort  —  Array [9,7,8,5,6,4,3,1,2]
Comparing adjacent pairs, swapping when out of order — watch the amber pair
  • 01

    The outer loop runs N−1 times. After pass i, the largest i elements are guaranteed to be in their final position at the end of the array.

  • 02

    The inner loop compares adjacent pairs array[j] and array[j+1]. If they are out of order, swap() exchanges them via pointers — no copies of the whole array needed.

  • 03

    The swapped flag provides an early-exit optimisation: if an entire pass completes without a swap, the array is already sorted and the loop breaks early.

BubbleSort.c
void swap(int* x, int* y) {
    int tmp = *x;
    *x = *y;
    *y = tmp;
}

void BubbleSort(int array[], int siz) {
    bool swapped;
    for (int i = 0; i < siz - 1; i++) {
        swapped = 0;
        for (int j = 0; j < siz - i - 1; j++) {
            if (array[j] > array[j+1]) {
                swap(&array[j], &array[j+1]);
                swapped = 1;
            }
        }
        if (!swapped) break; // already sorted
    }
}
II

Binary Conversion

Bitwise Shift Masking  ·  Base-2 Representation

16-Bit Live Display
Each bit extracted via right-shift and AND mask — lit squares are 1s
  • 01

    The loop iterates from bit position 15 down to 0. At each step, the number is right-shifted by i, bringing bit i to position 0.

  • 02

    The result is then ANDed with 1 — masking away all higher bits and leaving only the lowest bit, which is either 0 or 1.

  • 03

    No division or modulo is needed. Pure bitwise logic reads each bit in a single CPU instruction, illustrating how integers are physically stored in memory.

ConversionToBinary.c
void convert(int a) {
    for (int i = 15; i >= 0; i--) {
        int b = (a >> i) & 1; // shift bit i to pos 0, mask
        printf("%d", b);
    }
}

/*  Bitwise operators:
    >>  right shift  — divides by 2^n
    &   AND         — masks specific bits
    |   OR
    ^   XOR         — OR but 1&1 → 0
    ~   NOT
    <<  left shift  — multiplies by 2^n  */
III

Grade Calculator

Conditional Branching  ·  While Loop  ·  Char Output

Interactive Grade Meter
Drag the slider to see the letter grade update
Score (0 – 100) 85
  • 01

    The while loop runs until the user enters −1. Each iteration reads a numeric grade, processes it, and prints the letter — demonstrating a sentinel-controlled input loop.

  • 02

    Grade boundaries (≤59 F, ≤69 D, ≤79 C, ≤89 B, ≤100 A) are checked with chained else-if statements — once a condition matches, the rest are skipped.

  • 03

    The result is stored in a char variable — showing that characters in C are just small integers and can be assigned letter literals directly.

grade.c
int graden; char gradeC;

while (graden != -1) {
    printf("Enter your grade: ");
    scanf("%d", &graden);
    if      (graden <= 59)  gradeC = 'F';
    else if (graden <= 69)  gradeC = 'D';
    else if (graden <= 79)  gradeC = 'C';
    else if (graden <= 89)  gradeC = 'B';
    else if (graden <= 100) gradeC = 'A';
    printf("Your grade is: %c\n", gradeC);
}
IV

Insertion Sort

In-Place Insertion  ·  O(N²) Worst, O(N) Best

Live Sort  —  Array [5,6,7,4,3,5,2,1,4]
The green key slides left until it finds its sorted position
  • 01

    The outer loop picks each element starting from index 1 as the key. Everything to the left is already a sorted sub-array — the key must be inserted into it correctly.

  • 02

    The inner while loop shifts elements one position right as long as they are greater than the key. This opens up a slot for the key to drop into.

  • 03

    Insertion sort runs in O(N) time on a nearly-sorted array — the while condition fails immediately for each key, making it ideal as a final pass in hybrid algorithms like Timsort.

InsertionSort.c
void ISort(int* arr, int size, int* resultarr) {
    for (int i = 1; i < size; i++) {
        int j = i;
        while (j > 0 && arr[j] < arr[j-1]) {
            swap(arr, j, j-1); // shift right, key slides left
            j--;
        }
    }
    for (int i = 0; i < size; i++)
        resultarr[i] = arr[i];
}

void swap(int* arr, int i, int j) {
    int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
}
V

Reverse Array

Two-Pointer Swap  ·  O(N/2)  ·  In-Place

Live Reversal  —  Array [1 … 10]
Left and right pointers march inward, swapping pairs until they meet
  • 01

    Only N/2 iterations are needed. Index i starts at 0 and its mirror partner is always at size − i − 1, so each swap fixes two positions at once.

  • 02

    The swap uses a temporary variable — a classic three-step exchange. No extra array is allocated; the reversal is in-place, using O(1) additional memory.

  • 03

    This two-pointer pattern appears everywhere: palindrome checking, rotation, partitioning in quicksort. Understanding it is foundational to array algorithms.

ReverseArray.c
int ReverseNum(int a[], int size) {
    int temp = 0;
    for (int i = 0; i < size / 2; i++) {
        temp            = a[size - i - 1]; // save right
        a[size - i - 1] = a[i];           // right ← left
        a[i]            = temp;           // left  ← saved
    }
}