Explain various algorithms used in data structure.


Q.) Explain various algorithms used in data structure.

Subject: Data Structures - II

1. Sorting Algorithms:

  • Bubble Sort:
    • Compares adjacent elements and swaps them if they are in the wrong order.
    • Repeats until no more swaps are needed.
    • Simple and easy to implement, but inefficient for large datasets.
  • Selection Sort:
    • Finds the minimum element from the unsorted portion and swaps it with the leftmost unsorted element.
    • Repeats until the entire dataset is sorted.
    • More efficient than Bubble Sort, but not as efficient as other sorting algorithms.
  • Insertion Sort:
    • Builds the sorted array one element at a time by inserting each unsorted element into its correct position in the sorted portion.
    • Efficient for small datasets and nearly sorted datasets.
  • Merge Sort:
    • Divides the array into smaller subarrays, sorts them recursively, and merges them back together.
    • Efficient and stable, with a time complexity of O(n log n).
  • Quick Sort:
    • Selects a pivot element, partitions the array into two subarrays based on the pivot, and recursively applies Quick Sort to the subarrays.
    • Efficient on average, with a time complexity of O(n log n), but worst-case time complexity of O(n^2).

2. Searching Algorithms:

  • Linear Search:
    • Compares the target element with each element in the dataset sequentially.
    • Simple and easy to implement, but inefficient for large datasets.
  • Binary Search:
    • Works on sorted datasets and repeatedly divides the search space in half until the target element is found or the search space is exhausted.
    • Efficient for large sorted datasets, with a time complexity of O(log n).
  • Hashing:
    • Uses a hash function to map data items to key-value pairs.
    • Allows for constant-time lookup and insertion if the hash function is well-chosen.
    • Common hashing algorithms include MD5, SHA-1, and SHA-256.

3. Tree Data Structures:

  • Binary Tree:
    • A tree data structure where each node can have at most two child nodes.
    • Used for efficient searching and sorting.
  • Binary Search Tree:
    • A binary tree where the left child of each node is less than or equal to the node, and the right child is greater than or equal to the node.
    • Allows for efficient searching, insertion, and deletion.
  • AVL Tree:
    • A self-balancing binary search tree where the height of the left and right subtrees of each node differ by at most one.
    • Maintains balance and ensures efficient search and update operations.
  • Red-Black Tree:
    • Another self-balancing binary search tree with specific coloring rules to maintain balance.
    • Similar to AVL trees in terms of efficiency and balance.

4. Graph Data Structures:

  • Adjacency List:
    • Represents a graph as a collection of vertices, where each vertex has a list of its adjacent vertices.
    • Efficient for sparse graphs, where the average number of edges per vertex is low.
  • Adjacency Matrix:
    • Represents a graph as a two-dimensional matrix, where the entry at row i and column j indicates the weight of the edge between vertex i and vertex j.
    • Efficient for dense graphs, where the average number of edges per vertex is high.

5. Hash Tables:

  • Open Addressing:
    • Stores key-value pairs in an array, using techniques like linear probing, quadratic probing, or double hashing to resolve collisions.
    • Simple and efficient for small to medium-sized datasets.
  • Chaining:
    • Stores key-value pairs in an array of linked lists, where each linked list contains all key-value pairs with the same hash value.
    • Efficient for large datasets and datasets with a high collision rate.

These are just a few examples of the many algorithms used in data structures. The choice of algorithm depends on the specific requirements of the problem being solved, such as the size of the dataset, the operations that need to be performed, and the desired efficiency.