Voiced by Amazon Polly |
Introduction
Searching is a fundamental operation in computer science that involves finding specific values within a data collection.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Linear Search
The linear search algorithm is also known as the sequential search algorithm. A simple search algorithm checks each element in a collection until the desired value is found. Linear search can be used for sorted and unsorted collections, but it is useful for small collections.
Here’s an example of how to implement a linear search in C#:
1 2 3 4 5 6 7 8 9 10 11 |
public static int LinearSearch(int[] arr, int target) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == target) { return i; } } return -1; // target not found } |
The above implementation has a time complexity of O(n), where n is the collection size. As the collection size grows, the time to search the collection also grows linearly.
Binary Search
Binary search is a more efficient search algorithm that divides the collection in half at each iteration. Binary search requires the collection to be sorted in ascending or descending order.
Here’s an example of how to implement binary search in C#.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public static int BinarySearch(int[] arr, int target) { int left = 0; int right = arr.Length - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; // target not found } |
The above implementation has a time complexity of O(log n), where n is the collection size. As the collection size grows, the time taken to search the collection grows logarithmically.
Interpolation Search
Interpolation search is a variant of binary search that works best for uniformly distributed collections. It uses an interpolation formula to estimate the position of the target element.
Here’s an example of how to implement interpolation search in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static int InterpolationSearch(int[] arr, int target) { int left = 0; int right = arr.Length - 1; while (left <= right && target >= arr[left] && target <= arr[right]) { int pos = left + ((target - arr[left]) * (right - left)) / (arr[right] - arr[left]); if (arr[pos] == target) { return pos; } else if (arr[pos] < target) { left = pos + 1; } else { right = pos - 1; } } return -1; // target not found } |
The above implementation has a time complexity of O(√n), where n is the collection size. As the collection size grows, the time to search the collection grows slowly.
Jump Search
Jump search is another variant of binary search that works by jumping ahead by a fixed number of steps instead of dividing the interval in half.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public static int JumpSearch(int[] arr, int target) { int n = arr.Length; int step = (int)Math.Sqrt(n); int prev = 0; while (arr[Math.Min(step, n) - 1] < target) { prev = step; step += (int)Math.Sqrt(n); if (prev >= n) { return -1; // target not found } } while (arr[prev] < target) { prev++; if (prev == Math.Min(step, n)) { return -1; // target not found } } if (arr[prev] == target) { return prev; } return -1; // target not found } |
The above implementation has a time complexity of O(√n), where n is the collection size. As the collection size grows, the time to search the collection grows slowly.
Conclusion
Searching Algorithms are essential to computer science and have many practical applications. In this blog, we explored four different searching algorithms in C#: linear search, binary search, interpolation search, and jump search. Each algorithm has advantages and disadvantages, and the best algorithm to use depends on the application’s specific requirements.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. What are searching algorithms in C#?
ANS: – Searching algorithms in C# are designed to find the presence or location of a specific element within a data collection. These algorithms operate on various data structures, such as arrays, lists, trees, or hash tables, and provide efficient methods to locate the desired element.
2. What are some common searching algorithms in C#?
ANS: – There are several common searching algorithms implemented in C#. Some examples include:
- Linear Search
- Binary Search
- Interpolation Search
- Hashing (using hash tables)
- Breadth-First Search (BFS)
- Depth-First Search (DFS)

WRITTEN BY Subramanya Datta
Comments