Tag Archives: KMP Algorithm

๐Ÿ” Searching Algorithms


๐Ÿงฉ What is Searching?

Image
Image
Image
Image

Searching is the process of locating a specific element (called a key) within a data structure such as an array, list, tree, or graph. It is one of the most fundamental operations in computer science and forms the backbone of data retrieval systems.

Example:

Array: [10, 25, 30, 45, 60]
Search Key: 30 โ†’ Found at index 2

Searching algorithms are designed to efficiently determine:

  • Whether an element exists
  • Where it is located
  • How quickly it can be found

๐Ÿง  Importance of Searching Algorithms

  • Essential for data retrieval systems
  • Used in databases and search engines
  • Helps in decision-making algorithms
  • Improves performance of applications

โš™๏ธ Classification of Searching Algorithms

Searching algorithms can be categorized based on:

๐Ÿ”น 1. Based on Data Structure

  • Searching in arrays/lists
  • Searching in trees
  • Searching in graphs

๐Ÿ”น 2. Based on Technique

  • Sequential search
  • Divide and conquer
  • Hash-based search

๐Ÿ”น 3. Based on Data Order

  • Searching in unsorted data
  • Searching in sorted data

๐Ÿ”ข Linear Search

Image
Image
Image
Image

๐Ÿ“Œ Concept

Linear search checks each element one by one until the target is found.

๐Ÿงพ Algorithm

  1. Start from first element
  2. Compare with key
  3. Move to next element
  4. Repeat until found or end

๐Ÿ’ป Code Example

def linear_search(arr, key):
    for i in range(len(arr)):
        if arr[i] == key:
            return i
    return -1

โฑ๏ธ Complexity

  • Best: O(1)
  • Average: O(n)
  • Worst: O(n)

โœ… Advantages

  • Simple
  • Works on unsorted data

โŒ Disadvantages

  • Slow for large datasets

๐Ÿ” Binary Search

Image
Image
Image
Image

๐Ÿ“Œ Concept

Binary search repeatedly divides a sorted array into halves.

๐Ÿงพ Algorithm

  1. Find middle element
  2. Compare with key
  3. If equal โ†’ return
  4. If smaller โ†’ search left
  5. If larger โ†’ search right

๐Ÿ’ป Code Example

def binary_search(arr, key):
    low, high = 0, len(arr)-1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == key:
            return mid
        elif arr[mid] < key:
            low = mid + 1
        else:
            high = mid - 1
    return -1

โฑ๏ธ Complexity

  • Best: O(1)
  • Average: O(log n)
  • Worst: O(log n)

โœ… Advantages

  • Very fast
  • Efficient for large datasets

โŒ Disadvantages

  • Requires sorted data

๐Ÿง  Jump Search

Image
Image
Image

๐Ÿ“Œ Concept

Jumps ahead by fixed steps and then performs linear search.

โฑ๏ธ Complexity

  • O(โˆšn)

๐Ÿ”Ž Interpolation Search

Image
Image

๐Ÿ“Œ Concept

Estimates position based on value distribution.

โฑ๏ธ Complexity

  • Best: O(log log n)
  • Worst: O(n)

๐Ÿงญ Exponential Search

Image
Image
Image

๐Ÿ“Œ Concept

Finds range first, then applies binary search.


๐ŸŒณ Searching in Trees

Image
Image
Image
Image

๐Ÿ“Œ Binary Search Tree (BST)

Search based on ordering:

  • Left < Root < Right

โฑ๏ธ Complexity

  • Average: O(log n)
  • Worst: O(n)

๐ŸŒ Searching in Graphs


๐Ÿ”น Depth First Search (DFS)

Image
Image
Image
  • Uses stack
  • Explores deeply

๐Ÿ”น Breadth First Search (BFS)

Image
Image
Image
  • Uses queue
  • Explores level by level

๐Ÿ”‘ Hash-Based Searching

Image
Image
Image
Image

๐Ÿ“Œ Concept

Uses hash functions to map keys to positions.

โฑ๏ธ Complexity

  • Average: O(1)
  • Worst: O(n)

๐Ÿงฎ Comparison Table

AlgorithmBest CaseAverageWorst Case
Linear SearchO(1)O(n)O(n)
Binary SearchO(1)O(log n)O(log n)
Jump SearchO(1)O(โˆšn)O(โˆšn)
InterpolationO(1)O(log log n)O(n)
ExponentialO(1)O(log n)O(log n)
HashingO(1)O(1)O(n)

โšก Advantages of Searching Algorithms

  • Efficient data retrieval
  • Reduces computation time
  • Improves system performance

โš ๏ธ Disadvantages

  • Some require sorted data
  • Complex implementation
  • Extra memory usage (hashing)

๐Ÿง  Advanced Searching Concepts


๐Ÿ”น 1. Ternary Search

Image
Image
Image

Divides array into three parts.


๐Ÿ”น 2. Fibonacci Search

Image
Image
Image
Image

Uses Fibonacci numbers.


๐Ÿ”น 3. Pattern Searching

Image
Image
Image

Used in strings:

  • KMP
  • Rabin-Karp

๐Ÿ”ฌ Applications of Searching


๐ŸŒ 1. Search Engines

Image
Image
Image
Image

๐Ÿงพ 2. Databases

Image
Image
Image
Image

๐Ÿง  3. Artificial Intelligence

Image
Image
Image
Image

๐ŸŽฎ 4. Games

Image
Image
Image
Image

๐Ÿ“Š 5. Data Analytics

Image
Image
Image
Image

๐Ÿ” Searching vs Sorting

FeatureSearchingSorting
PurposeFind elementArrange elements
DependencyOften needs sortingIndependent

๐Ÿงช Real-World Importance

Searching algorithms are essential in:

  • Web applications
  • Databases
  • Networking
  • AI systems
  • Cybersecurity

๐Ÿงพ Conclusion

Searching algorithms are critical for efficient data handling and retrieval. From simple linear search to advanced hashing and AI-based search methods, they form the backbone of modern computing systems.

Mastering searching algorithms enables:

  • Faster problem solving
  • Efficient coding
  • Strong algorithmic thinking

๐Ÿท๏ธ Tags

๐Ÿงฉ Arrays and Strings โ€“ Complete Detailed Guide


๐ŸŒ Introduction to Arrays and Strings

Image
Image
Image
Image

Arrays and strings are among the most fundamental data structures in computer science and programming. They form the building blocks for more complex structures like lists, stacks, queues, trees, and databases.

  • Array โ†’ Stores a collection of elements of the same data type
  • String โ†’ Stores a sequence of characters (text)

In simple terms:

Arrays manage collections of data, while strings manage textual data


๐Ÿง  ARRAYS


๐Ÿ“Œ What is an Array?

An array is a data structure that stores multiple elements of the same type in contiguous memory locations.

Example:

int arr[5] = {10, 20, 30, 40, 50};

โš™๏ธ Characteristics of Arrays

  • Fixed size (in most languages)
  • Homogeneous elements (same type)
  • Indexed access (0-based index)
  • Stored in contiguous memory

๐Ÿงฉ Array Representation in Memory

Image
Image
Image
Image

Each element is stored sequentially:

Index:   0   1   2   3   4
Value:  10  20  30  40  50

Address calculation:

Address = Base + (Index ร— Size of element)

๐Ÿ”ข Types of Arrays


๐Ÿ”น 1. One-Dimensional Array

Image
Image
Image
Image
  • Linear structure
  • Single index

๐Ÿ”น 2. Two-Dimensional Array

Image
Image
Image
Image
  • Matrix format
  • Rows and columns

Example:

int arr[2][3];

๐Ÿ”น 3. Multi-Dimensional Array

Image
Image
Image
Image
  • Used in scientific computing
  • Example: 3D arrays

โš™๏ธ Array Operations


๐Ÿ”น Traversal

  • Access each element

๐Ÿ”น Insertion

  • Add element (costly if fixed size)

๐Ÿ”น Deletion

  • Remove element and shift

๐Ÿ”น Searching

  • Linear search
  • Binary search

๐Ÿ”น Sorting

  • Bubble sort
  • Merge sort
  • Quick sort

๐Ÿ” Searching Techniques

Image
Image
Image
Image

โšก Advantages of Arrays

  • Fast access (O(1))
  • Simple implementation
  • Efficient memory usage

โš ๏ธ Limitations of Arrays

  • Fixed size
  • Insertion/deletion costly
  • Wasted memory

๐Ÿ”ค STRINGS


๐Ÿ“Œ What is a String?

A string is a sequence of characters stored in memory.

Example:

char str[] = "Hello";

๐Ÿง  String Representation

Image
Image
Image
Image

Stored as:

H  e  l  l  o  \0

(\0 = null terminator)


๐Ÿ”ค Character Encoding


๐Ÿ”น ASCII

Image
Image
Image
Image
  • 7/8-bit encoding
  • Limited characters

๐Ÿ”น Unicode

Image
Image
Image
Image
  • Supports global languages
  • UTF-8, UTF-16

โš™๏ธ String Operations


๐Ÿ”น Basic Operations

  • Length
  • Concatenation
  • Comparison
  • Substring

๐Ÿ”น Advanced Operations

Image
Image
Image
Image
  • Pattern matching
  • Parsing
  • Tokenization

๐Ÿ” String Searching Algorithms


๐Ÿ”น Naive Algorithm

๐Ÿ”น KMP Algorithm

๐Ÿ”น Rabin-Karp Algorithm


๐Ÿ”„ Arrays vs Strings


โš–๏ธ Comparison Table

FeatureArrayString
Data TypeAnyCharacters
SizeFixedVariable
UsageGeneral dataText

๐Ÿง  Memory Management


๐Ÿ“ฆ Static vs Dynamic Arrays

  • Static โ†’ Fixed size
  • Dynamic โ†’ Resizable

Example:

  • Python lists
  • Java ArrayList

๐Ÿง  Dynamic Strings

  • Strings can be mutable or immutable

โš™๏ธ Multidimensional Strings


๐Ÿงฉ Examples:

  • Array of strings
  • String matrices

๐Ÿง  Applications of Arrays and Strings


๐Ÿ’ป Programming

  • Data storage
  • Algorithms

๐ŸŒ Web Development

  • Text processing
  • Input handling

๐Ÿค– AI and Data Science

  • Data representation
  • NLP (Natural Language Processing)

๐ŸŽฎ Gaming

  • Graphics arrays
  • Text rendering

โšก Advantages


Arrays:

  • Fast access
  • Structured storage

Strings:

  • Easy text manipulation
  • Human-readable

โš ๏ธ Limitations


Arrays:

  • Fixed size
  • Less flexible

Strings:

  • Memory overhead
  • Slower operations

๐Ÿš€ Advanced Topics

Image
Image
Image
Image
  • Dynamic arrays
  • String hashing
  • Suffix arrays
  • Advanced data structures

๐Ÿงพ Conclusion

Arrays and strings are core data structures in computing. They:

  • Store and organize data
  • Enable efficient algorithms
  • Form the basis of advanced programming

Understanding them is essential for:

  • Coding interviews
  • Software development
  • Algorithm design

๐Ÿท๏ธ Tags