0% found this document useful (0 votes)
1 views48 pages

Data Structures Study Guide

The document provides a comprehensive study guide on algorithms and data structures, defining algorithms as step-by-step procedures for solving problems. It outlines key characteristics of algorithms, various algorithm paradigms such as Divide and Conquer, Dynamic Programming, and Greedy Algorithms, and explains linear and non-linear data structures. Additionally, it covers specific linear data structures like arrays, linked lists, stacks, and queues, along with sorting and searching algorithms, including Bubble Sort and Binary Search.

Uploaded by

barryqueen182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views48 pages

Data Structures Study Guide

The document provides a comprehensive study guide on algorithms and data structures, defining algorithms as step-by-step procedures for solving problems. It outlines key characteristics of algorithms, various algorithm paradigms such as Divide and Conquer, Dynamic Programming, and Greedy Algorithms, and explains linear and non-linear data structures. Additionally, it covers specific linear data structures like arrays, linked lists, stacks, and queues, along with sorting and searching algorithms, including Bubble Sort and Binary Search.

Uploaded by

barryqueen182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Data Structures Study

Guide
1. What is an algorithm?
• An algorithm is a step-by-step procedure or set of rules designed to
perform a specific task or solve a particular problem in a finite
amount of time. Algorithms are the foundation of all computer
programs and data processing.
Characteristics of an algorithm
1. Finiteness
•The algorithm must always terminate after a finite number of steps.
2. Definiteness (Clarity)
•Each step must be clearly and unambiguously defined.
3. Input
•An algorithm should have zero or more well-defined inputs.
4. Output
•It must produce at least one output — a result that solves the problem.
5. Effectiveness
•All steps must be basic enough to be carried out, in principle, by a human using pen and
paper.
6. Generality
•The algorithm should be applicable to a broad set of input values, not just specific cases.
Algorithm paradigms
• Algorithm paradigms are general strategies or approaches for
solving computational problems.
• They are like blueprints or design patterns that guide how
algorithms are constructed.
• Instead of focusing on the specific steps of one algorithm,
paradigms describe a high-level way to think about solving
problems — especially in terms of:
• How the problem is broken down
• How sub-problems relate to each other
• How solutions are constructed and combined
Algorithm paradigms
• 1. Divide and Conquer
• Idea: Break a problem into smaller sub-problems, solve each recursively, and
combine the results.
• Steps:
• Divide the problem
• Conquer (solve sub-problems)
• Combine the solutions
• Examples:
• Merge Sort
• Quick Sort
• Binary Search
• Strassen’s Matrix Multiplication
Algorithm paradigms
• 2. Dynamic Programming (DP)
• Idea: Solve complex problems by breaking them into overlapping sub-
problems and storing solutions to avoid recomputation.
• Key Technique: Memoization or Tabulation
• Used When: Sub-problems repeat and can be reused
• Examples:
• Fibonacci sequence
• Knapsack problem
• Longest Common Subsequence (LCS)
• Matrix Chain Multiplication
Algorithm paradigms
• 3. Greedy Algorithms
• Idea: Make the best local choice at each step, hoping it leads to the
global optimum.
• Used When: The problem has the greedy-choice property and
optimal substructure
• Examples:
• Dijkstra’s Algorithm
• Prim’s Algorithm
• Kruskal’s Algorithm
• Activity Selection
Algorithm paradigms
• 4. Backtracking
• Idea: Try solving a problem step-by-step, abandoning ("backtracking")
a path as soon as it’s known to be invalid.
• Used For: Decision problems, puzzles, constraint satisfaction
• Examples:
• N-Queens Problem
• Sudoku Solver
• Maze Solving
• Subset Sum
Algorithm paradigms
• 5. Brute Force
• Idea: Try all possible solutions and select the best one.
• Simple but inefficient
• Often used as a baseline for comparison
• Examples:
• Generate all permutations/combinations
• Password cracking
• Linear search
2. Data Structures
• A data structure is a way of organizing and storing data in a
computer so that it can be accessed and modified efficiently.
• The Link Between Algorithms and Data Structures
• Data structures are the containers for storing data.
• Algorithms are the procedures for processing that data.
Together, they make software efficient and effective.
Linear and Non-Linear Data
Structures
• Data structures are broadly classified into Linear and Non-linear
based on how data elements are organized and accessed.
Differences Between Linear and Non-linear Data Structures
Feature Linear Data Structures Non-linear Data Structures

Organization Data elements are arranged in a Data elements are arranged


sequence (one after another) hierarchically or graph-like

Traversal Traversed linearly, one element Traversed using various paths (e.g.,
after the other DFS, BFS)
Memory Allocation Contiguous memory (often) Non-contiguous, dynamic memory

Complexity Generally simpler to implement More complex in terms of


and manage operations and relationships

Performance Flexibility Less flexible with complex More flexible and powerful for
relationships modeling real-world structures
Linear Data Structures
In linear data structures, elements are arranged sequentially, and each
element is connected to its previous and next element (except the first
and last).
Key Characteristics:
• Data is organized in a single level
• Traversal is done in a single run (e.g., left to right)
• Memory is often contiguously allocated
Linear Data Structures
Common Linear Data Structures:
Structure Description Example Use Case
Fixed-size collection of elements
Array Storing scores, sensor data
stored in order
Linked List Each element points to the next Dynamic memory, playlist
Stack LIFO (Last In First Out) Undo/redo, backtracking
Queue FIFO (First In First Out) Print queue, task scheduling
Linear Data Structures
Array:
Array is a linear data structure where all elements are arranged sequentially.
It is a collection of elements of same data type stored at contiguous memory locations.

Index in an Array
•The index refers to the position of an element in the array.
•If arr = [3, 4, 10, 5, 15, 3], then:
•arr[0] = 3
•arr[1] = 4
•arr[3] = 10
•arr[4] = 5
•arr[5] = 15
•arr[6] = 3
Linear Data Structures
• Arrays allow efficient access and manipulation of data, making them
ideal for implementing common algorithms such as:
• Sorting algorithms (e.g., bubble sort)
• Searching algorithms (e.g., linear and binary search)
Linear Data Structures
• Arrays (Sorting algorithms)
• Bubble Sort
Bubble Sort is a simple sorting algorithm that works by repeatedly swapping
adjacent elements if they are in the wrong order. The largest unsorted
element "bubbles up" to its correct position at the end of the array in each
pass.
• How It Works (Step-by-Step)
• Compare the first two elements.
• Swap them if the first is greater than the second.
• Move to the next pair, and repeat.
• After each full pass, the largest element is at the end — sorted.
• Repeat the process for the remaining unsorted part.
Linear Data Structures
• Bubble Sort
• Example
Given the array [64, 34, 25, 12, 22, 11, 90], perform a bubble sort. Show the array after
each pass and highlight the sorted portion.
• Pass 1
• [64, 34, 25, 12, 22, 11, 90] → Compare 64 and 34 → swap
• [34, 64, 25, 12, 22, 11, 90] → Compare 64 and 25 → swap
• [34, 25, 64, 12, 22, 11, 90] → Compare 64 and 12 → swap
• [34, 25, 12, 64, 22, 11, 90] → Compare 64 and 22 → swap
• [34, 25, 12, 22, 64, 11, 90] → Compare 64 and 11 → swap
• [34, 25, 12, 22, 11, 64, 90] → Compare 64 and 90 → no swap

• Result after Pass 1:


• [34, 25, 12, 22, 11, 64, 90]
Linear Data Structures
• Bubble Sort
• Example
Given the array [64, 34, 25, 12, 22, 11, 90], perform a bubble sort. Show the array
after each pass and highlight the sorted portion.
• Pass 2
• [34, 25, 12, 22, 11, 64, 90] → 34 > 25 → swap
• [25, 34, 12, 22, 11, 64, 90] → 34 > 12 → swap
• [25, 12, 34, 22, 11, 64, 90] → 34 > 22 → swap
• [25, 12, 22, 34, 11, 64, 90] → 34 > 11 → swap
• [25, 12, 22, 11, 34, 64, 90] → 34 < 64 → no swap

• Result after Pass 2:


• [25, 12, 22, 11, 34, 64, 90]
Linear Data Structures
• Bubble Sort
• Example
Given the array [64, 34, 25, 12, 22, 11, 90], perform a bubble sort. Show the
array after each pass and highlight the sorted portion.
• Pass 3
• [25, 12, 22, 11, 34, 64, 90] → 25 > 12 → swap
• [12, 25, 22, 11, 34, 64, 90] → 25 > 22 → swap
• [12, 22, 25, 11, 34, 64, 90] → 25 > 11 → swap
• [12, 22, 11, 25, 34, 64, 90] → 25 < 34 → no swap

• Result after Pass 3:


• [12, 22, 11, 25, 34, 64, 90]
Linear Data Structures
• Bubble Sort
• Example
Given the array [64, 34, 25, 12, 22, 11, 90], perform a bubble sort. Show
the array after each pass and highlight the sorted portion.
• Pass 4
• [12, 22, 11, 25, 34, 64, 90] → 12 < 22 → no swap
• [12, 22, 11, 25, 34, 64, 90] → 22 > 11 → swap
• [12, 11, 22, 25, 34, 64, 90] → 22 < 25 → no swap

• Result after Pass 4:


• [12, 11, 22, 25, 34, 64, 90]
Linear Data Structures
• Bubble Sort
• Example
Given the array [64, 34, 25, 12, 22, 11, 90], perform a bubble sort.
Show the array after each pass and highlight the sorted portion.
• Pass 5
• [12, 11, 22, 25, 34, 64, 90] → 12 > 11 → swap
• [11, 12, 22, 25, 34, 64, 90] → all other comparisons are in order

• Result after Pass 5:


• [11, 12, 22, 25, 34, 64, 90]
Linear Data Structures
• Bubble Sort
• Example
Given the array [64, 34, 25, 12, 22, 11, 90], perform a bubble sort.
Show the array after each pass and highlight the sorted portion.
• Pass 6
• (No swaps — already sorted)

• Final sorted array:


• [11, 12, 22, 25, 34, 64, 90]
Linear Data Structures
Arrays (Searching algorithms)
• Linear Search
In Linear Search, we iterate over all the elements of the array and
check if it the current element is equal to the target element. If we
find any element to be equal to the target element, then return the
index of the current element. Otherwise, if no element is equal to the
target element, then return -1 as the element is not found. Linear
search is also known as sequential search.
• For example: Consider the array arr = [10, 50, 30] and key = 30
Linear Data Structures
Arrays (Searching algorithms)
• Linear Search
For example: Consider the array arr = [10, 50, 30] and key = 30

Step 1: Compare key with first element which is arr[0]. arr[0] means element at index 0 which is 10

Key

30 Not Equal

0 1 2

10 50 30
Linear Data Structures
Arrays (Searching algorithms)
• Linear Search
For example: Consider the array arr = [10, 50, 30] and key = 30

Step 2: Compare key with first element which is arr[1]. arr[1] means element at index 1 which is 50

Key

30 Not Equal

0 1 2

10 50 30
Linear Data Structures
Arrays (Searching algorithms)
• Linear Search
For example: Consider the array arr = [10, 50, 30] and key = 30

Step 2: Compare key with first element which is arr[2]. arr[2] means element at index 2 which is 30

Key

30 Equal

0 1 2

10 50 30
Linear Data Structures
Arrays (Searching algorithms)
• Linear Search
Pseudocode for Linear Search
function linear_search (arr, target):
n = Length(arr)
For i ← 0 to n - 1 do
If arr[i] = target then
Return i
End For
Return -1
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
Binary Search is an efficient searching algorithm used to find the
position of a target value in a sorted array by repeatedly dividing the
search interval in half.

• Key Conditions
• The array must be sorted (in ascending or descending order).
• Binary search works by comparing the target value to the middle element of
the array.
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
How Binary Search Works
• (Step-by-Step)
• Start with two pointers: low = 0, high = length - 1.
• Find the middle index: mid = (low + high) // 2.
• Compare arr[mid] with the target:
• If equal → target found!
• If target < arr[mid] → search left half.
• Low = low , high = mid- 1
• If target > arr[mid] → search right half.
• Low = mid + 1, high = high
• Repeat until the element is found or low > high.
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
How Binary Search Works
• To understand the working of binary search, consider the following
illustration:
• Consider an array arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and
the target = 23.
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
How Binary Search Works
• Consider an array arr = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91], and
the target = 23.
• Step 1 : Calculate mid = (low + high) // 2
• mid = 0 + 9//2 = 4

0 1 2 3 4 5 6 7 8 9

2 5 8 12 16 23 38 56 72 91

low high
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
How Binary Search Works
• Step 2 : Compare arr[mid] with target
16 with 23
Key

23 Not Equal (23 > 16 , search space moves to right


0 1 2 3 4
low =5 5 and high
6
=9 7 8 9

2 5 8 12 16 23 38 56 72 91

low high
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
How Binary Search Works
• Step 3: Calculate mid = 5 + 9//2 = 7
• Step 4 : Compare arr[mid] with target
56 with 23
Key
Not Equal (23 < 56 , search space moves to left
23 low = 5 and high =6
0 1 2 3 4 5 6 7 8 9

2 5 8 12 16 23 38 56 72 91

low high
Linear Data Structures
Arrays (Searching algorithms)
• Binary Search
How Binary Search Works
• Step 5: Calculate mid = 5 + 6//2 = 5
• Step 6 : Compare arr[mid] with target
23 with 23
Key

23 Equal (element found)

0 1 2 3 4 5 6 7 8 9

2 5 8 12 16 23 38 56 72 91

low high
Linear Data Structures
Stack
A Stack is a linear data structure that follows a particular order in which
the operations are performed. The order may be LIFO(Last In First
Out) or FILO(First In Last Out). LIFO implies that the element that is
inserted last, comes out first and FILO implies that the element that is
inserted first, comes out last.
• It behaves like a stack of plates, where the last plate added is the first
one to be removed. Think of it this way:
• Pushing an element onto the stack is like adding a new plate on top.
• Popping an element removes the top plate from the stack.
Linear Data Structures
Stack
• Pushing an element onto the stack is like adding a new plate on top.
• Popping an element removes the top plate from the stack.
Linear Data Structures
Stack
• Pushing an element onto the stack is like adding a new plate on top.
• Popping an element removes the top plate from the stack.
Stack Operations
Operation Description
Push Add (insert) an element to the top
Pop Remove the top element
Peek/Top View the top element without removing it
isEmpty Check if the stack is empty
isFull (if size is limited) Check if the stack is full
Linear Data Structures
Queue
• Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a
specific order.
• It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first
one to be removed.
• Front
• The first element in the queue.
• This is where dequeue operations happen — the element is removed from the front.

• Rear
• The last position in the queue.
• This is where enqueue operations happen — the element is added at the rear.
Linear Data Structures
Queue
Types of Queues
Type Description
Simple Queue FIFO structure
Circular Queue Reuses space after elements are dequeued
Priority Queue Elements are served based on priority, not order
Deque (Double-ended queue) Insertion/removal at both ends
Linear Data Structures
Queue
Types of Queues
Circular Queue
• A circular queue is a type of queue in which the last position is connected back to the first position to make a
circle. It is also known as a Ring Buffer. In a normal queue, once the queue becomes full, we cannot insert
the next element even if there is a space in front of the queue. But using a circular queue, we can use the
space to insert elements.
• It is a linear data structure that follows the FIFO mechanism. The circular queue is a more efficient way to
implement a queue in a fixed size array. In a circular queue, the last element points to the first element
making a circular link.
Linear Data Structures
Linked List
• A linked list is a linear data structure where elements (called nodes) are linked using pointers. Each node
contains:
• Data
• A pointer (next) to the next node

Image below shows a singly linked list with:


• A Head pointing to the first node.
• The first node pointing to the second node
• The second node pointing to NULL (end of list)
Linear Data Structures
Linked List
Insert a New Node Between the Two Existing Nodes

•Node1: the first node (currently after the head)


•Node2: the second node (currently at the end)
•NewNode: the node to insert between them

•If we want to insert a new node after a specific node, we first locate that node. Once we find it, we set the new node’s
next reference to point to the node that follows the given node. Then, we update the given node’s next to point to
the new node. This requires traversing the list to find the specified node.
Linear Data Structures
Linked List
Insert a New Node Between the Two Existing Nodes
Step 1: Create New Node With its own data and next pointer

Head Next Next


Data Items Data Items

NULL

Data Items Next

New Node
Linear Data Structures
Linked List
Insert a New Node Between the Two Existing Nodes
Step 2: Point New Node’s next to Node2

Head Next Next


Data Items Data Items

NULL

Data Items Next


Linear Data Structures
Linked List
Insert a New Node Between the Two Existing Nodes
Step 3: Update Node1’s next to NewNodeNode

Head Next Next


Data Items Data Items

NULL

Data Items Next


Linear Data Structures
Linked List
Insert a New Node Between the Two Existing Nodes
Step 3: Update Node1’s next to NewNodeNode

Head Next Next


Data Items Data Items

NULL

Data Items Next


Non Linear Data Structures
Tree
A tree is a non-linear hierarchical data structure made up of nodes
connected by edges. It is used to represent data with a parent-child
relationship, like folders in a computer or family ancestry.
Term Description
Node The fundamental unit of a tree (contains data)
Root The topmost node of the tree
Child A node directly connected and below another node
Parent A node that has one or more children
Leaf A node with no children
Edge The link between parent and child
Subtree Any node and its descendants
Depth Distance from root to a node
Height Distance from a node to its deepest leaf
Non Linear Data Structures
Tree
A tree is a non-linear hierarchical data structure made up of nodes
connected by edges. It is used to represent data with a parent-child
relationship, like folders in a computer or family ancestry.
Root node

Edges

A,B,C,D.E,F,G,H,I,R are nodes

Example edges are RC, CF,RA,CH


A line that join two nodes

Depth of a tree (also called height of the tree) is the maximum


depth among all nodes.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy