diff --git a/app/sem2/dsc/[chapter]/page.tsx b/app/sem2/dsc/[chapter]/page.tsx index 78d6572..b675575 100644 --- a/app/sem2/dsc/[chapter]/page.tsx +++ b/app/sem2/dsc/[chapter]/page.tsx @@ -9,6 +9,7 @@ import { Ch3Content } from "../content/chapter3"; import { Ch4Content } from "../content/chapter4"; import { Ch5Content } from "../content/chapter5"; import { Ch6Content } from "../content/chapter6"; +import { Ch7Content } from "../content/chapter7"; import { ArrowBigLeft, ArrowBigRight } from "lucide-react"; import { moduleQuizzes } from "@/lib/quizData"; @@ -29,6 +30,7 @@ const chapters = [ { id: "ch4", title: "Queues", component: Ch4Content }, { id: "ch5", title: "Trees", component: Ch5Content }, { id: "ch6", title: "Graphs", component: Ch6Content }, + { id: "ch7", title: "Searching", component: Ch7Content }, ]; type ChapterProps = { diff --git a/app/sem2/dsc/components/sidebar.tsx b/app/sem2/dsc/components/sidebar.tsx index fc2e3e5..07c8740 100644 --- a/app/sem2/dsc/components/sidebar.tsx +++ b/app/sem2/dsc/components/sidebar.tsx @@ -28,6 +28,7 @@ export default function Sidebar() { { id: "ch4", title: "Queues" }, { id: "ch5", title: "Trees" }, { id: "ch6", title: "Graphs" }, + { id: "ch7", title: "Searching" }, ]; const quizSlugMap: Record = { diff --git a/app/sem2/dsc/content/chapter0.tsx b/app/sem2/dsc/content/chapter0.tsx index 1fe509f..f646af6 100644 --- a/app/sem2/dsc/content/chapter0.tsx +++ b/app/sem2/dsc/content/chapter0.tsx @@ -103,8 +103,17 @@ export const Ch0Content = () => {

Module VII: Searching

diff --git a/app/sem2/dsc/content/chapter7.tsx b/app/sem2/dsc/content/chapter7.tsx new file mode 100644 index 0000000..96f40ee --- /dev/null +++ b/app/sem2/dsc/content/chapter7.tsx @@ -0,0 +1,1315 @@ +export const Ch7Content = () => { + return ( +
+ +

+ + Module VII: Searching. + + Searching is one of the most fundamental operations in Computer Science. + It is used to determine whether a particular element exists in a dataset + and to locate its position if it is present. Efficient searching techniques + help retrieve information quickly from large collections of data and are + widely used in databases, search engines, operating systems, file systems, + e-commerce platforms, and many other software applications. +

+ +
+ + {/* Introduction */} +
+ +

+ Introduction to Searching +

+ +

+ Searching is the process of finding whether a particular element exists + in a collection of data and determining its location if it is present. + It is one of the most fundamental operations performed on data + structures. +

+ +

+ Searching algorithms help retrieve information efficiently from a + dataset. They are widely used in databases, search engines, operating + systems, file systems, and software applications. +

+ +

+ The choice of a searching algorithm depends on factors such as data + organization, dataset size, and performance requirements. +

+ +
    +
  • Used to locate specific elements in a dataset
  • +
  • Helps retrieve information quickly
  • +
  • Improves application performance
  • +
  • Can be applied to various data structures
  • +
  • Forms the basis of many advanced algorithms
  • +
+ +
+ +
+ + {/* Need for Searching */} +
+ +

+ Need for Searching +

+ +

+ As the amount of stored data increases, locating a specific element + manually becomes difficult and time-consuming. Searching algorithms + provide systematic methods for finding information quickly and + accurately. +

+ +

+ Efficient searching helps reduce processing time, improves user + experience, and enhances the overall performance of applications that + handle large datasets. +

+ +
    +
  • Finding student records in a database
  • +
  • Searching contacts in a mobile phone
  • +
  • Locating files in a file system
  • +
  • Searching products in an online store
  • +
  • Retrieving information from search engines
  • +
  • Verifying the existence of data before processing
  • +
+ +
+ +
+ + {/* Characteristics of Searching */} +
+ +

+ Characteristics of Searching +

+ +

+ Searching algorithms are designed to locate specific elements within a + dataset efficiently. Different searching techniques provide different + levels of performance depending on the organization of data and the + algorithm being used. +

+ +
    +
  • Used to determine whether an element exists in a dataset
  • +
  • May return the position of the element if found
  • +
  • Can be performed on sorted or unsorted data
  • +
  • Different algorithms provide different efficiencies
  • +
  • Performance is measured using time and space complexity
  • +
  • Widely used in databases and software applications
  • +
+ +
+ +
+ + {/* Linear Search */} +
+ +

+ Linear Search +

+ +

+ Linear Search is the simplest searching technique in which elements are + checked one by one from the beginning of the dataset until the required + element is found or the end of the dataset is reached. +

+ +

+ It can be used on both sorted and unsorted data. Since every element + may need to be examined, Linear Search is less efficient for large + datasets when compared to Binary Search. +

+ +

+ Working of Linear Search +

+ +
    +
  • Start from the first element
  • +
  • Compare the current element with the target value
  • +
  • If a match is found, return its position
  • +
  • If not, move to the next element
  • +
  • Repeat until the element is found or the array ends
  • +
+ +

+ Example +

+ +
+ +
+                        {`Array:
+
+10 20 30 40 50
+
+Search Element: 40
+
+Step 1:
+10 ≠ 40
+
+Step 2:
+20 ≠ 40
+
+Step 3:
+30 ≠ 40
+
+Step 4:
+40 = 40
+
+Element Found`}
+                    
+ +
+ +

+ Algorithm +

+ +
+ +
+                        {`Step 1: Start
+
+Step 2: Read array and target value
+
+Step 3: Compare each element
+        with the target
+
+Step 4: If element matches,
+        return position
+
+Step 5: Otherwise continue
+
+Step 6: If end of array is reached,
+        element is not present
+
+Step 7: Stop`}
+                    
+ +
+ +

+ C Program +

+ +
+ +
+                        {`#include
+
+int linearSearch(
+    int arr[],
+    int n,
+    int key){
+
+    for(int i=0;
+        i
+
+                
+ +

+ Complexity Analysis +

+ +
+ +
+                        {`Case              Time Complexity
+
+Best Case              O(1)
+
+Average Case           O(n)
+
+Worst Case             O(n)
+
+Space Complexity       O(1)`}
+                    
+ +
+ +

+ Advantages +

+ +
    +
  • Simple and easy to implement
  • +
  • Works on both sorted and unsorted data
  • +
  • Does not require additional memory
  • +
  • Suitable for small datasets
  • +
+ +

+ Disadvantages +

+ +
    +
  • Inefficient for large datasets
  • +
  • May require checking every element
  • +
  • Slower than Binary Search for sorted data
  • +
+ +
+ +
+ + {/* Sentinel Linear Search */} +
+ +

+ Sentinel Linear Search +

+ +

+ Sentinel Linear Search is an improved version of Linear Search that + reduces the number of comparisons performed during searching. It works + by placing the target element temporarily at the end of the array, + known as the Sentinel. +

+ +

+ By using a Sentinel, the algorithm avoids checking whether the current + index has reached the end of the array during every iteration, + resulting in a slightly more efficient implementation. +

+ +

+ Working of Sentinel Linear Search +

+ +
    +
  • Store the last element of the array
  • +
  • Place the target element at the last position
  • +
  • Start searching from the beginning of the array
  • +
  • Stop when the target element is encountered
  • +
  • Restore the original last element
  • +
  • Determine whether the element was originally present
  • +
+ +

+ Example +

+ +
+ +
+                        {`Array:
+
+10 20 30 40 50
+
+Search Element: 40
+
+Store Last Element = 50
+
+Place Sentinel:
+
+10 20 30 40 40
+
+Search Starts
+
+10 ≠ 40
+
+20 ≠ 40
+
+30 ≠ 40
+
+40 = 40
+
+Element Found`}
+                    
+ +
+ +

+ C Program +

+ +
+ +
+                        {`#include
+
+int sentinelSearch(
+    int arr[],
+    int n,
+    int key){
+
+    int last=
+    arr[n-1];
+
+    arr[n-1]=key;
+
+    int i=0;
+
+    while(arr[i]!=key)
+        i++;
+
+    arr[n-1]=last;
+
+    if(i
+
+                
+ +

+ Complexity Analysis +

+ +
+ +
+                        {`Case              Time Complexity
+
+Best Case              O(1)
+
+Average Case           O(n)
+
+Worst Case             O(n)
+
+Space Complexity       O(1)`}
+                    
+ +
+ +

+ Advantages +

+ +
    +
  • Reduces the number of comparisons
  • +
  • Slightly more efficient than Linear Search
  • +
  • Easy to implement
  • +
  • Works on unsorted data
  • +
+ +

+ Disadvantages +

+ +
    +
  • Temporarily modifies the array
  • +
  • Still has linear time complexity
  • +
  • Less commonly used in practical applications
  • +
+ +
+ +
+ + {/* Binary Search */} +
+ +

+ Binary Search +

+ +

+ Binary Search is an efficient searching algorithm used to find an + element in a sorted array. Instead of checking every element one by + one, Binary Search repeatedly divides the search space into two halves + until the required element is found. +

+ +

+ Since Binary Search eliminates half of the remaining elements during + each comparison, it performs much faster than Linear Search for large + sorted datasets. +

+ +

+ Prerequisite +

+ +

+ Binary Search works only on sorted data. If the array is not sorted, + the algorithm may produce incorrect results. +

+ +
+ +
+                        {`Sorted Array:
+
+10 20 30 40 50 60 70`}
+                    
+ +
+ +

+ Working of Binary Search +

+ +
    +
  • Find the middle element of the array
  • +
  • Compare the middle element with the target value
  • +
  • If both are equal, the element is found
  • +
  • If the target is smaller, search the left half
  • +
  • If the target is larger, search the right half
  • +
  • Repeat until the element is found or the search space becomes empty
  • +
+ +

+ Example +

+ +
+ +
+                        {`Array:
+
+10 20 30 40 50 60 70
+
+Search Element: 60
+
+Step 1:
+
+Middle = 40
+
+60 > 40
+
+Move Right
+
+
+Step 2:
+
+50 60 70
+
+Middle = 60
+
+Element Found`}
+                    
+ +
+ +

+ Algorithm +

+ +
+ +
+                        {`Step 1: Start
+
+Step 2: Initialize
+        low = 0
+        high = n - 1
+
+Step 3: Find middle element
+
+Step 4: Compare middle
+        with target value
+
+Step 5: If equal,
+        element found
+
+Step 6: If target is smaller,
+        search left half
+
+Step 7: If target is larger,
+        search right half
+
+Step 8: Repeat until
+        low > high
+
+Step 9: Stop`}
+                    
+ +
+ +

+ C Program +

+ +
+ +
+                        {`#include
+
+int binarySearch(
+    int arr[],
+    int n,
+    int key){
+
+    int low=0;
+    int high=n-1;
+
+    while(low<=high){
+
+        int mid=
+        low +
+        (high-low)/2;
+
+        if(arr[mid]==key)
+            return mid;
+
+        if(key
+
+                
+ +

+ Complexity Analysis +

+ +
+ +
+                        {`Case              Time Complexity
+
+Best Case              O(1)
+
+Average Case           O(log n)
+
+Worst Case             O(log n)
+
+Space Complexity       O(1)`}
+                    
+ +
+ +

+ Advantages +

+ +
    +
  • Much faster than Linear Search for large datasets
  • +
  • Efficient due to logarithmic time complexity
  • +
  • Requires no extra memory in iterative implementation
  • +
  • Widely used in searching applications
  • +
+ +

+ Disadvantages +

+ +
    +
  • Works only on sorted data
  • +
  • Sorting may require additional processing time
  • +
  • Less suitable for linked lists due to lack of direct indexing
  • +
+ +
+ +
+ + {/* Recursive Binary Search */} +
+ +

+ Recursive Binary Search +

+ +

+ Recursive Binary Search is a variation of Binary Search that uses + recursion to repeatedly divide the search space into smaller halves. + Instead of using loops, the function calls itself until the element is + found or the search range becomes empty. +

+ +

+ The algorithm follows the same logic as Binary Search. The only + difference is that recursion is used to process the left or right half + of the array. +

+ +

+ Working of Recursive Binary Search +

+ +
    +
  • Find the middle element of the current search range
  • +
  • If the middle element matches the target, return its position
  • +
  • If the target is smaller, recursively search the left half
  • +
  • If the target is larger, recursively search the right half
  • +
  • Stop when the element is found or the search range becomes empty
  • +
+ +

+ Example +

+ +
+ +
+                        {`Array:
+
+10 20 30 40 50 60 70
+
+Search Element: 20
+
+Step 1:
+
+Middle = 40
+
+20 < 40
+
+Search Left Half
+
+
+Step 2:
+
+10 20 30
+
+Middle = 20
+
+Element Found`}
+                    
+ +
+ +

+ C Program +

+ +
+ +
+                        {`#include
+
+int recursiveBinarySearch(
+    int arr[],
+    int low,
+    int high,
+    int key){
+
+    if(low>high)
+        return -1;
+
+    int mid=
+    low +
+    (high-low)/2;
+
+    if(arr[mid]==key)
+        return mid;
+
+    if(key
+
+                
+ +

+ Complexity Analysis +

+ +
+ +
+                        {`Case              Time Complexity
+
+Best Case              O(1)
+
+Average Case           O(log n)
+
+Worst Case             O(log n)
+
+Space Complexity       O(log n)`}
+                    
+ +
+ +

+ The space complexity is O(log n) because recursive function calls are + stored in the system call stack. +

+ +

+ Advantages +

+ +
    +
  • Simple and elegant implementation
  • +
  • Easy to understand divide-and-conquer approach
  • +
  • Efficient for large sorted datasets
  • +
+ +

+ Disadvantages +

+ +
    +
  • Works only on sorted data
  • +
  • Uses additional stack memory
  • +
  • Slightly slower than iterative Binary Search due to function calls
  • +
+ +
+ +
+ + {/* Comparison of Searching Techniques */} +
+ +

+ Comparison of Searching Techniques +

+ +

+ Different searching algorithms offer different levels of performance. + The choice of a searching technique depends on factors such as whether + the data is sorted, dataset size, and efficiency requirements. +

+ +
+ +
+                        {`Feature              Linear      Sentinel     Binary
+
+Sorted Data
+Required             No          No           Yes
+
+Best Case            O(1)        O(1)         O(1)
+
+Average Case         O(n)        O(n)         O(log n)
+
+Worst Case           O(n)        O(n)         O(log n)
+
+Space
+Complexity           O(1)        O(1)         O(1)
+
+Implementation
+Difficulty           Easy        Easy         Moderate
+
+Suitable For
+Large Datasets       No          No           Yes`}
+                    
+ +
+ +

+ Linear Search +

+ +
    +
  • Works on both sorted and unsorted data
  • +
  • Simple to implement
  • +
  • Suitable for small datasets
  • +
  • May require checking every element
  • +
+ +

+ Sentinel Linear Search +

+ +
    +
  • Improves Linear Search by reducing comparisons
  • +
  • Works on unsorted data
  • +
  • Still requires linear time in the worst case
  • +
  • Less commonly used in real-world applications
  • +
+ +

+ Binary Search +

+ +
    +
  • Requires sorted data
  • +
  • Highly efficient for large datasets
  • +
  • Uses divide-and-conquer strategy
  • +
  • Provides logarithmic search performance
  • +
+ +
+ +
+ + {/* Complexity Analysis of Searching Techniques */} +
+ +

+ Complexity Analysis of Searching Techniques +

+ +

+ Complexity Analysis helps evaluate the efficiency of searching + algorithms by measuring the amount of time and memory required to + perform a search operation. +

+ +

+ Time Complexity indicates how the execution time grows as the size of + the dataset increases, while Space Complexity represents the amount of + additional memory used by the algorithm. +

+ +
+ +
+                        {`Algorithm                Best       Average      Worst
+
+Linear Search          O(1)        O(n)        O(n)
+
+Binary Search          O(1)      O(log n)    O(log n)
+
+Recursive
+Binary Search          O(1)      O(log n)    O(log n)
+
+Sentinel Search        O(1)        O(n)        O(n)`}
+                    
+ +
+ +

+ Space Complexity Comparison +

+ +
+ +
+                        {`Algorithm                Space Complexity
+
+Linear Search               O(1)
+
+Binary Search               O(1)
+
+Recursive
+Binary Search             O(log n)
+
+Sentinel Search             O(1)`}
+                    
+ +
+ +

+ Binary Search provides significantly better performance than Linear + Search for large datasets because it repeatedly reduces the search + space by half. However, Binary Search requires the data to be sorted + before searching can be performed. +

+ +

+ Recursive Binary Search has the same time complexity as Iterative + Binary Search but requires additional stack memory for recursive + function calls. +

+ +

+ Sentinel Search slightly reduces the number of comparisons compared to + Linear Search but still has linear time complexity in the average and + worst cases. +

+ +
+ +
+ + {/* Applications of Searching */} +
+ +

+ Applications of Searching +

+ +

+ Searching algorithms are widely used in computer systems to locate, + retrieve, and manage information efficiently. Almost every software + application relies on searching operations to provide quick access to + data. +

+ +

+ Efficient searching reduces response time and improves the overall + performance of applications handling large amounts of data. +

+ +
    +
  • Database record retrieval
  • +
  • Search engines
  • +
  • Contact management systems
  • +
  • Library management systems
  • +
  • File and folder searching
  • +
  • E-commerce product search
  • +
  • Student information systems
  • +
  • Operating systems
  • +
  • Dictionary and spell-check applications
  • +
  • Online reservation systems
  • +
+ +
+ +
+ + {/* Advantages of Searching Algorithms */} +
+ +

+ Advantages of Searching Algorithms +

+ +
    +
  • Provides quick access to information
  • +
  • Reduces manual effort in locating data
  • +
  • Improves software performance
  • +
  • Supports efficient data management
  • +
  • Enables fast retrieval from large datasets
  • +
  • Forms the basis of many advanced algorithms
  • +
+ +
+ +
+ + {/* Limitations of Searching Algorithms */} +
+ +

+ Limitations of Searching Algorithms +

+ +
    +
  • Some algorithms require sorted data
  • +
  • Performance may decrease for very large unsorted datasets
  • +
  • Sorting data may introduce additional overhead
  • +
  • Efficiency depends on the organization of data
  • +
  • Different applications require different searching techniques
  • +
+ +
+ +
+ + {/* Summary */} +
+ +

+ Summary +

+ +

+ Searching is the process of locating a specific element within a + dataset. It is one of the most frequently performed operations in + computer systems and plays a vital role in data retrieval and + information management. +

+ +

+ Linear Search examines elements sequentially and can be applied to both + sorted and unsorted data. Binary Search improves efficiency by + repeatedly dividing a sorted dataset into smaller halves. Recursive + Binary Search applies the same divide-and-conquer strategy using + recursion, while Sentinel Search optimizes Linear Search by reducing + unnecessary comparisons. +

+ +

+ The choice of a searching algorithm depends on the structure and size + of the dataset. For large sorted datasets, Binary Search provides + significantly better performance than Linear Search due to its + logarithmic time complexity. +

+ +
+ +
+ + {/* Interview Questions */} +
+ +

+ Interview Questions +

+ +

+ 1. What is Searching? +

+ +

+ Searching is the process of finding whether a particular element exists + in a dataset and determining its location. +

+ +

+ 2. What is Linear Search? +

+ +

+ Linear Search is a searching technique that checks elements one by one + until the target element is found or the dataset ends. +

+ +

+ 3. What is Binary Search? +

+ +

+ Binary Search is an efficient searching algorithm that repeatedly + divides a sorted dataset into two halves until the target element is + found. +

+ +

+ 4. Why does Binary Search require sorted data? +

+ +

+ Binary Search relies on the ordering of elements to determine whether + to search the left half or the right half of the dataset. +

+ +

+ 5. What is the best-case time complexity of Linear Search? +

+ +

+ O(1), when the target element is found at the first position. +

+ +

+ 6. What is the worst-case time complexity of Linear Search? +

+ +

+ O(n), when the element is located at the last position or is not + present in the dataset. +

+ +

+ 7. What is the average-case time complexity of Binary Search? +

+ +

+ O(log n). +

+ +

+ 8. What is Sentinel Linear Search? +

+ +

+ Sentinel Linear Search is a variation of Linear Search that places the + target element temporarily at the end of the array to reduce the number + of comparisons. +

+ +

+ 9. What is the space complexity of Iterative Binary Search? +

+ +

+ O(1). +

+ +

+ 10. What is the space complexity of Recursive Binary Search? +

+ +

+ O(log n) due to recursive function calls stored in the call stack. +

+ +

+ 11. Which searching algorithm is best for large sorted datasets? +

+ +

+ Binary Search. +

+ +

+ 12. Can Binary Search be used on unsorted arrays? +

+ +

+ No. Binary Search requires the data to be sorted. +

+ +

+ 13. Which searching algorithm can be used on unsorted data? +

+ +

+ Linear Search and Sentinel Linear Search. +

+ +

+ 14. What is the main advantage of Binary Search over Linear Search? +

+ +

+ Binary Search has O(log n) time complexity, making it significantly + faster for large sorted datasets. +

+ +

+ 15. Give two real-world applications of Searching. +

+ +

+ Database record retrieval and search engines. +

+ +
+ +
+ ); +};