๐งฑ 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.
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).
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
AnimationStack and Queue are used in almost every program. Logic gates are the physical circuits that execute binary operations.
Data Structures & Algorithms Explorer
InteractiveExample: 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ยฒ)
โข 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)