Computer ยท Chapter 09

๐Ÿงฑ Data Structures & Logic Gates

Stack, queue, sorting algorithms, logic gates.

๐Ÿงฑ Building Blocks of Computing

Data Structures organize data for efficient access and modification. Choosing the right structure is key to writing efficient programs.

Linear structures (sequential):
โ€ข Array โ€” fixed-size, same-type elements. Index-based. O(1) access. O(n) search.
โ€ข Linked List โ€” nodes with data + pointer to next. Dynamic size. O(n) access. Efficient insert/delete.
โ€ข Stack โ€” LIFO (Last In First Out). Push (add) and Pop (remove) from top. Used in: undo/redo, recursion, browser back button.
โ€ข Queue โ€” FIFO (First In First Out). Enqueue (add) at rear, Dequeue (remove) from front. Used in: print queue, task scheduling, BFS.

Non-linear structures:
โ€ข Tree โ€” hierarchical. Binary tree: max 2 children. BST (Binary Search Tree): left < root < right. Used in file systems, databases.
โ€ข Graph โ€” vertices connected by edges. Directed/Undirected, Weighted. Used in maps, social networks.

โšก Sorting algorithms โ€” O-Level

Bubble Sort โ€” compare adjacent, swap if wrong order. O(nยฒ). Simple but slow.
Selection Sort โ€” find minimum, place at beginning. O(nยฒ).
Insertion Sort โ€” insert each element into sorted portion. O(nยฒ). Good for nearly sorted.
Merge Sort โ€” divide and conquer. O(n log n). Stable.
Quick Sort โ€” pivot-based. O(n log n) average, O(nยฒ) worst. Fastest in practice.
Binary Search โ€” on sorted array. O(log n). Linear Search O(n).

๐Ÿ”Œ Logic Gates โ€” digital circuits

AND gate โ€” output 1 only if ALL inputs are 1. Symbol: D-shape.
OR gate โ€” output 1 if ANY input is 1.
NOT gate (Inverter) โ€” flips input. 0โ†’1, 1โ†’0.
NAND gate โ€” NOT AND. Universal gate โ€” any circuit can be built with only NAND gates.
NOR gate โ€” NOT OR. Also universal gate.
XOR gate โ€” output 1 if inputs are DIFFERENT.
XNOR gate โ€” output 1 if inputs are SAME.
Boolean algebra: AยทB (AND), A+B (OR), A' or ยฌA (NOT)

๐ŸŽฌ

Stack vs Queue โ€” Visualized

Animation
DATA STRUCTURES โ€” CLICK PUSH/ENQUEUE TO ANIMATE STACK (LIFO) Last In, First Out โ€” like a plate stack 10 20 30 โ† TOP โ† PUSH here โ† POP from here PUSH โ†‘ POP โ†“ Used in: Undo/Redo, Recursion, Browser Back, Expression eval QUEUE (FIFO) First In, First Out โ€” like a ticket line A B C โ† DEQUEUE (front) ENQUEUE โ†’ (rear) ENQUEUE โ†’ โ† DEQUEUE Used in: Print queue, CPU scheduling, Keyboard buffer, BFS traversal LOGIC GATES โ€” TRUTH TABLES AND Gate 0ยท0=0 | 0ยท1=0 1ยท0=0 | 1ยท1=1 Output 1 only if ALL=1 OR Gate 0+0=0 | 0+1=1 1+0=1 | 1+1=1 Output 1 if ANY=1 NOT Gate 0 โ†’ 1 1 โ†’ 0 Inverts the input NAND Gate 0ยท0=1 | 0ยท1=1 1ยท0=1 | 1ยท1=0 Universal gate XOR Gate 0โŠ•0=0 | 0โŠ•1=1 1โŠ•0=1 | 1โŠ•1=0 Output 1 if DIFFERENT NOR Gate 0+0=1 | 0+1=0 1+0=0 | 1+1=0 Universal gate Click PUSH/ENQUEUE/POP/DEQUEUE buttons to see operations

Stack and Queue are used in almost every program. Logic gates are the physical circuits that execute binary operations.

๐Ÿ’ป

Data Structures & Algorithms Explorer

Interactive
O(1)Constant โ€” array access by index
O(log n)Logarithmic โ€” binary search (halves each step)
O(n)Linear โ€” linear search, single loop
O(n log n)Merge Sort, Quick Sort (avg)
O(nยฒ)Quadratic โ€” Bubble, Selection, Insertion sort
Practice (O-Level): Explain Bubble Sort with an example.
Bubble Sort โ€” repeatedly compares adjacent elements and swaps them if in wrong order. Largest element "bubbles up" to the end in each pass.

Example: Sort [5, 3, 8, 1, 4]

Pass 1:
[5,3,8,1,4] โ†’ compare 5,3 โ†’ swap โ†’ [3,5,8,1,4]
[3,5,8,1,4] โ†’ compare 5,8 โ†’ no swap
[3,5,8,1,4] โ†’ compare 8,1 โ†’ swap โ†’ [3,5,1,8,4]
[3,5,1,8,4] โ†’ compare 8,4 โ†’ swap โ†’ [3,5,1,4,8] โœ“ 8 is in place

Pass 2:
[3,5,1,4,8] โ†’ [3,1,4,5,8] โœ“ 5 is in place

Pass 3:
[3,1,4,5,8] โ†’ [1,3,4,5,8] โœ“ 4 is in place

Final: [1,3,4,5,8] โ€” sorted!

Time complexity: O(nยฒ) โ€” n-1 passes, each up to n-1 comparisons
Space complexity: O(1) โ€” in-place sorting
Stable sort: Yes โ€” equal elements maintain relative order

Best case (already sorted): O(n) with optimization flag
Worst case: O(nยฒ)
Practice (SSC/CCC): What is the difference between a stack and a queue? Give real-life examples.
Stack โ€” LIFO (Last In, First Out):
โ€ข Last element added is the first to be removed
โ€ข Operations: PUSH (add to top), POP (remove from top), PEEK (view top)
โ€ข Like a stack of plates โ€” you add and remove from the top

Real-life examples:
โ€ข Ctrl+Z (Undo) โ€” last action is undone first
โ€ข Browser Back button โ€” last page visited is first to go back
โ€ข Function call stack โ€” last function called returns first (recursion)
โ€ข Expression evaluation โ€” bracket matching

Queue โ€” FIFO (First In, First Out):
โ€ข First element added is the first to be removed
โ€ข Operations: ENQUEUE (add to rear), DEQUEUE (remove from front)
โ€ข Like a ticket queue โ€” first person in line is first served

Real-life examples:
โ€ข Print queue โ€” documents print in order received
โ€ข CPU scheduling (Round Robin) โ€” processes wait in queue
โ€ข Keyboard buffer โ€” keystrokes processed in order typed
โ€ข WhatsApp message queue โ€” messages delivered in order
โ€ข BFS (Breadth First Search) in graphs uses a queue

Key difference: Stack = LIFO (recent items first), Queue = FIFO (older items first)
โ†
Previous
Cybersecurity