Partial preview of the text
Download Data Structures & Algorithms Notes – Arrays, Linked Lists, Stacks, Queues, Trees & Graphs and more Study notes Data Structures and Algorithms in PDF only on Docsity!
Data Structures 1. ARRAYS ° An array is a collection of elements of same data type stored in contiguous memory locations. Characteristics of Array. e Elements are accessed using an index . . Indexing in most languages starts from 0. Example : A= [10, 20, 30, 40, 50] Index : fo) 1 2 3 4 A — | 10 | 20 | 30 | 40 | 50 Types Aree e 1D Array - linear List ° 2D Array - table (vows x columns) © Multi-dimensional Arrays 2D Array Example (3 x 3) 4 2 3 —= Row 0 4 5 6 — Row 1 “i 8 9 — Row 2 my Ff Col O Colt Col 2 2. LINKED LISTS Feature Description Memory Contiguous block of memory Access Random access by index Search Linear / Binary Search Insertion/ Costly (in middle, Deletion elements need to be shifted) Size Fixed size (in static arrays) ° A linked list is a Linear data structure where elements (nodes) ave not stored at contiguous memory locations . * Each node contains two parts : 1) Data 2) Address (link to next node) Types : Singly Linked List, Doubly Linked List, Circular Linked List Sin Linked List Head 10 . 20 e 30 40 -—— NULL if Data ‘ Link Doubly Linked List NULL e 10 20 30 | e ¢ 40 t NULL t t Head Tail Circular Linked List Head ——>| 10 -—>| 20 30 40 — t Basic Operations on Linked List Type When to Use ¢ Traverse / Displa : Poy Singly Tinked iiee Simple structure , forward. ¢ Insertion (at beginning ,end, or any position) traversal only ¢ Deletion (from beginning, end, or any position) Doubly Linked. List Nestilt Laced eemeren © Search efficiently ei Count Circular Linked List | Continuous traversal (e.g., round vobin scheduling) 3. STACKS A stack is a linear data structure that follows LIFO (Lost In First Out) principle. Insertion and deletion are allowed only at one end called TOP. Main operations : Push, Pop, Peek, isEmpty, isFull Stack ti tack Operations Stack Operations | Time Complexit pe | TOP» -4 Push 2 ‘ » mee |e a top> 1. | Pop O(1) 20 20 20 so Le = i Peek/ Top oO(4) Empty Stack Push(40) Push(20) Push(30) Pop() aie ah! 4. QUEUES © A queue is a linear date structure that follows FIFO (First In First Out) principle. @ Insertion is done at REAR and deletion is done at FRONT. © Main operations : Enqueue , Dequeue , Front, isEmpty, isFull Queue Operations Queue Operations | Time Complexity Empty Queue Enqueue (10) Enqueue (20) Enqueue (30) Dequeue() Enquene 04) 40 40 | 20 40 | 20 | 30 20 | 30| | Peau a) Front 0(1) Fe- Re-1 FeO R=0 F=O R=d FeO R=2 Fat Rad isEmpty / isFull 0(4) Types of Queues © Simple Queue - Basie FIFO queue * Circular Queue — Last position is connected to first to moke a circle * Deque (Double Ended Queue) — Insertion and deletion allowed at both ends . SORTING * Sorting is the process of arranging data in a particular order (ascending / descending). + It improves searching efficiency * Common sorting algorithms : 8. SEARCHING + Searching is the process of finding a given element in a collection. © Types of searching 1) Linear Search * Check each element one by one Algorithm | Best Case | Avg. Case | Worst Case | Space | Stable Geel thel clement te found Bubble Sort O(n) O(n?) O(n*) o(1) Yes * Works on both sorted and Selection Sort | O(n?) | O(n?) | Oln?) | o(1) | N ee . ok a: cs i: ss + Time Complexity : O(n) = at a ¥. Insertion Sort | O(n) O(n*) O(n?) Ot) es 2) Bi Sean Merge Sort | O(nlogn) | O(n logn) | O(n logn) | O(n) | Yes + Repeatedly divide the sorted Quick Sort | O(n logn) | On logn) | OCn*) | OClogn) | Ne oar ete ee nee © Compare middle element with Heap Sort | O(n logn) | O(n legn) | O(mlegn) | OC1) | Ne key and decide the holf. * Works only on sorted dota. Bubble Sort (ideo) : Repeatedly compare odjacent + Time Complexity : O(log n) Seon ey a ae ia Binary Search (idea) Example : Sort [ 5, 1, 4, 2, 8] in ascending order. Find middle element Pass 2: Of 4, 4; 2,5, 8 (8 ot last) Poss 2: [ 1, 2, 4, 5, 8 ] (5 ot last) Yes Poss 3: [ 14, 2, 4, 5,8] Csorted) Pel za No Sorting vs Searchi Feoture Sorting Searching key < middle hey > middle Meaning Arranging data in order | Finding a specific element a ‘S search in left half right half Purpose Organize data Locate data When used Before searching / for When position of element better arrangement is unknown. Time (general) Higher (depends on Linear : O(n) algorithm) Binary : O(log n) Summary of Time Complexities Data Structure / Algorithm Time Complexity Data Structure / Algorithm Time Complexity Access in Array o(1) Insert in Linked List (beginning) oO(4) Search in Unsorted Array O(n) Delete in Linked List (given node) o(1) Search in Sorted Array (Binary) O(log n) Push / Pop in Stack o(4) Insert in Array (at end) O(1) amortized. Enqueue / Dequeue in Queue o(4) Insert in Array (at position) O(n) Tree Traversal (all nodes) O(n) Delete from Array (at position) O(n) Graph Traversal (BFS / DFS) O(v +E) Where , number of elements, number of edges. V = n= E = number of vertices (nodes),