Algorithms (Copy)
Understanding Linear and Binary Searching Methods
Linear Search
- A simple searching algorithm that sequentially checks each element of a list.
- Starts from the first element and continues until the desired element is found or the list ends.
- Suitable for unordered lists.
- Pseudocode Example:
- Declare variables for list, bounds, and the search item.
- Set lower and upper bounds.
- Iterate through the list comparing elements with the target item.
- Stop if the item is found or the list ends.
- Time Complexity: O(n) in the worst case, where n is the number of elements.
Binary Search
- Works on sorted lists.
- Uses a divide-and-conquer approach:
- Compares the middle element with the target.
- If the target is smaller, search continues in the left half; otherwise, in the right half.
- Pseudocode Example:
- Declare variables for list, bounds, and target.
- While the lower bound is not greater than the upper bound:
- Compute the middle index.
- Compare the middle element with the target.
- Adjust bounds accordingly.
- Time Complexity: O(log n) in the worst case, making it significantly more efficient than linear search for large datasets.
Sorting Algorithms
Insertion Sort
- Sorts data by taking one element at a time and inserting it in the correct position.
- Steps:
- Start with the second element.
- Compare it with previous elements and shift larger elements to the right.
- Place the current element in its correct position.
- Repeat for all elements.
- Time Complexity: O(n^2) in the worst case, making it inefficient for large datasets.
- Performance:
- Performs well on small or nearly sorted lists.
- Requires fewer swaps compared to bubble sort.
Bubble Sort
- Compares adjacent elements and swaps them if they are in the wrong order.
- Steps:
- Iterate through the list.
- Swap elements if needed.
- Repeat the process until no swaps are required.
- Time Complexity: O(n^2) in the worst case.
- Performance:
- Very inefficient for large datasets.
- Performs more swaps than insertion sort.
Abstract Data Types (ADTs) and Their Operations
Linked Lists
- A collection of nodes where each node contains data and a pointer to the next node.
- Operations:
- Finding an item.
- Inserting an item.
- Deleting an item.
- Example:
- Define linked list structure.
- Implement search, insert, and delete functions in pseudocode.
Stacks
- Follows LIFO (Last In, First Out) principle.
- Operations:
- Push (inserting an item at the top).
- Pop (removing the top item).
- Implementation in different languages:
- Python, Java, and VB examples provided.
Queues
- Follows FIFO (First In, First Out) principle.
- Operations:
- Enqueue (adding an item at the end).
- Dequeue (removing an item from the front).
- Implementation in different languages:
- Python, Java, and VB examples provided.
Graph Representations and Applications
- Graphs consist of nodes (vertices) and edges (connections).
- Types of Graphs:
- Directed and Undirected.
- Weighted and Unweighted.
- Applications:
- GPS navigation.
- Social networks.
- Computer networks.
Big O Notation for Algorithm Comparison
- Used to describe the efficiency of an algorithm.
- Common Complexities:
- O(1) – Constant time.
- O(log n) – Logarithmic time (e.g., Binary search).
- O(n) – Linear time (e.g., Linear search).
- O(n^2) – Quadratic time (e.g., Bubble sort, Insertion sort).
- O(2^n) – Exponential time (e.g., Recursive algorithms for some problems).
Recursion in Programming
- A function that calls itself until a base condition is met.
- Example:
- Factorial calculation.
- Fibonacci sequence.
- Advantages:
- Simplifies complex problems.
- Reduces the need for explicit loops.
- Disadvantages:
- Can lead to excessive memory usage.
- Risk of stack overflow if not properly controlled.
Conclusion
- Chapter 19.1 covers essential algorithmic concepts.
- Understanding search and sorting methods is crucial for efficient data processing.
- ADTs help manage data effectively through stacks, queues, and linked lists.
- Graphs play a significant role in various applications.
- Big O notation aids in comparing algorithm performance.
- Recursion is a powerful but memory-intensive technique.
