Homework Scrolls · Chapter I
Selected Studies in Craft & Logic
Comparison-Based Sorting · O(N²)
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.
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.
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.
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 } }
Bitwise Shift Masking · Base-2 Representation
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.
The result is then ANDed with 1 — masking away all higher bits and leaving only the lowest bit, which is either 0 or 1.
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.
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 */
Conditional Branching · While Loop · Char Output
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.
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.
The result is stored in a char variable — showing that characters in C are just small integers and can be assigned letter literals directly.
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); }
In-Place Insertion · O(N²) Worst, O(N) Best
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.
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.
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.
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; }
Two-Pointer Swap · O(N/2) · In-Place
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.
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.
This two-pointer pattern appears everywhere: palindrome checking, rotation, partitioning in quicksort. Understanding it is foundational to array algorithms.
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 } }