0% found this document useful (0 votes)
54 views14 pages

Ada 1 PDF

Uploaded by

Alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views14 pages

Ada 1 PDF

Uploaded by

Alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

ABSTRACT

This document outlines the implementation and comparison of two search algorithms, Binary
Search and Interpolation Search, in the C programming language. Both algorithms are designed to
efficiently locate a target element within a sorted array, but they employ different strategies to
achieve this. Binary Search works by repeatedly dividing the search interval in half, making it a
robust and reliable method with a time complexity of O(log n). This algorithm is particularly
effective for uniformly distributed datasets and is straightforward to implement.

In contrast, Interpolation Search improves upon Binary Search by estimating the likely position of
the target element based on the values at the ends of the search interval. This estimation allows the
algorithm to potentially skip over large portions of the array, resulting in an average time
complexity of O(log log n) for uniformly distributed data. However, the performance of
Interpolation Search can degrade to O(n) in cases where the data is not uniformly distributed,
making it less predictable than Binary Search in such scenarios.

By implementing both algorithms in C, we can directly compare their performance and suitability
for different types of datasets. The implementations involve defining functions that execute the
respective search processes and testing these functions on a sample array. The results demonstrate
the conditions under which each algorithm excels, providing insights into their practical
applications and helping developers choose the most appropriate search method for their specific
needs.
2. INTRODUCTION

Searching algorithms are fundamental tools in computer science, used to locate specific
elements within data structures like arrays, linked lists, or databases. In C programming, these
algorithms play a crucial role in various applications, from simple data retrieval tasks to complex
database management and information retrieval systems. The efficiency of a searching algorithm
can significantly impact the performance of an application, making it essential to choose the
appropriate algorithm based on the nature and distribution of the data.

Two common searching algorithms implemented in C are Binary Search and Interpolation Search.
Binary Search operates on sorted arrays and uses a divide-and-conquer strategy to reduce the
search space by half with each iteration, providing a time complexity of O(log n). This makes it
highly efficient for uniformly distributed datasets. Interpolation Search, on the other hand,
improves upon Binary Search for certain types of datasets by estimating the likely position of the
target element based on its value, offering an average-case time complexity of O(log log n) for
uniformly distributed data. Both algorithms highlight the importance of understanding data
characteristics and distribution when selecting the most suitable search method in C programming.

Binary Search:
Binary Search is a fundamental search algorithm used to locate a target element
within a sorted array efficiently. The algorithm operates on the principle of divide-and-conquer,
systematically halving the search interval based on the comparison between the target element and
the middle element of the current interval. This method ensures that the search space is reduced
by half with each iteration, leading to a logarithmic time complexity of O(log n), where n is the
number of elements in the array. Due to its efficiency and simplicity, Binary Search is widely used
in various applications where quick data retrieval is essential.

Implementing Binary Search in C involves writing a function that takes the array, its boundaries
(low and high indices), and the target value as inputs. The function then iteratively or recursively
divides the search interval until the target element is found or the interval is exhausted.
The algorithm's performance is consistent and reliable, making it a preferred choice for searching
in sorted datasets. Its predictable efficiency ensures that it can handle large datasets effectively,
provided the data remains sorted.

Interpolation Search:

Interpolation Search is an advanced search algorithm that aims to improve the efficiency
of Binary Search for certain types of datasets. Unlike Binary Search, which always splits the array
into two equal parts, Interpolation Search estimates the position of the target element based on the
values at the boundaries of the current search interval. This estimation leverages the distribution
of the data, allowing the algorithm to potentially skip large portions of the array, resulting in faster
search times on average. The average-case time complexity of Interpolation Search is O(log log
n) for uniformly distributed data, making it a compelling choice for large datasets with such
characteristics.

The implementation of Interpolation Search in C involves calculating the estimated position using
a formula that considers the values of the low and high indices and the target value. The algorithm
then compares the target value with the value at the estimated position and adjusts the search
interval accordingly. While this method can offer significant performance benefits, its efficiency
can degrade to O(n) in the worst-case scenario, particularly when the data distribution is highly
skewed or clustered. Despite this potential drawback, Interpolation Search remains a valuable tool
for scenarios where the data distribution is approximately uniform, providing faster search
capabilities compared to Binary Search.
3. METHODOLOGY

1.Problem Definition :

The goal of this study is to implement and compare the performance of Binary Search and
Interpolation Search algorithms using the C programming language. The comparison will focus
on their efficiency in terms of time complexity and practical execution time, particularly in sorted
arrays with different distributions.

2. Algorithm Implementation

For Binary Search

❖ Definition:
Implement the Binary Search algorithm, which divides the search interval in half
repeatedly.
❖ Procedure:
i) Start with the entire array.
ii) Calculate the middle index.
iii) Compare the target element with the element at the middle index.
iv) If they match, return the index.
v) If the target is less than the middle element, repeat the search on the left half.
vi) If the target is greater, repeat the search on the right half.

❖ Time Complexity: O (log n)

For Interpolation Search

❖ Definition:
Implement the Interpolation Search algorithm, which estimates the position of the target
element based on its value relative to the values at the boundaries of the search interval.
❖ Procedure:
1. Start with the entire array.

2. Estimate the position using the formula:

pos = low + ((key - arr[low]) * (high - low)) / (arr[high] - arr[low]).

3. Compare the target element with the element at the estimated position.

4. If they match, return the index.

5. If the target is less than the estimated position element, repeat the search on the left half.

6. If the target is greater, repeat the search on the right half.

❖ Time Complexity: O(log log n) on average, O(n) in the worst case.

3. Experimental Setup

❖ Data Preparation:
Create several sorted arrays of different sizes (e.g., 100, 1,000, 10,000 elements).

Ensure arrays have varying distributions: uniform, skewed, and clustered.

❖ Implementation:
Write C functions for both Binary Search and Interpolation Search.

Use consistent input arrays for both functions to ensure a fair comparison.

4. Performance Measurement

❖ Execution Time:
Measure the time taken by each algorithm to search for a target element in the arrays.

Use the clock() function in C to record the start and end times of each search.

❖ Number of Comparisons:
Count the number of comparisons made during each search.
❖ Edge Cases:
Test both algorithms with edge cases such as:

The target element at the beginning, middle, and end of the array.

The target element not present in the array.

5. Analysis

❖ Time Complexity Analysis:


Compare the theoretical time complexities with the measured execution times.

❖ Efficiency Comparison:
Analyze the performance of both algorithms across different array sizes and distributions.

❖ Reliability:
Assess the reliability of each algorithm, especially for non-uniform distributions.
4. ALGORITHM
For Binary Search:

❖ Initialization:
• low and high are initialized to the bounds of the array.
❖ Main Loop:
• The loop continues while the search range is valid (low <= high).
• The middle index (mid) is calculated.
• Checking the middle position:
• If arr[mid] equals the search key x, the position is returned.
• If arr[mid] is greater than x, the search continues in the left subarray.
• If arr[mid] is less than x, the search continues in the right subarray.
❖ Termination:
• If the loop exits without finding the element, -1 is returned indicating the element is not
present in the array.
For Interpolation Search:

Initialization:

• low and high are initialized to the bounds of the array.

Main Loop:

• The loop continues while the search range is valid (low <= high and the target value is
within the range [arr[low], arr[high]]).

• The estimated position (pos) is calculated using the interpolation formula.

Checking the estimated position:

• If arr[pos] equals the search key x, the position is returned.

• If arr[pos] is less than x, the search continues in the right subarray.If arr[pos] is greater than
x, the search continues in the left subarray.

Termination: If the loop exits without finding the element, -1 is returned indicating the element
is not present in the array.
5. 1→SOURCE CODE FOR BINARY SEARCH:
#include <stdio.h>

int binarySearch(int arr[], int n, int x) {

int low = 0, high = n - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] == x) {

return mid; }

if (arr[mid] < x) {

low = mid + 1; }

else {

high = mid - 1; }

return -1;}

int main() {

int arr[] = {3,11,18,25,40,58,63,77,90,100 }; int x =63 ;

int n = sizeof(arr) / sizeof(arr[0]);

int result = binarySearch(arr, n, x);

if(result == -1) {

printf("Element is not present in array\n"); }

else {

printf("Element is present at index %d\n", result);

}
2→SOURCE CODE FOR Interpolation Search :
#include <stdio.h>

int interpolationSearch(int arr[], int n, int x) {

int low = 0, high = n - 1;

while (low <= high && x >= arr[low] && x <= arr[high]) {

if (low == high) {

if (arr[low] == x) return low;

return -1; }

int pos = low + ((double)(high - low) / (arr[high] - arr[low])) * (x - arr[low]);

if (arr[pos] == x) {

return pos; }

if (arr[pos] < x) {

low = pos + 1;}

else {

high = pos - 1; } }

return -1; }

int main() {

int arr[] = {1,3,5,7,9,11,13,15};

int n = sizeof(arr) / sizeof(arr[0]); int x = 9 ;

int result = interpolationSearch(arr, n, x);

if(result == -1) { printf("Element is not present in array\n"); }

else { printf("Element is present at index %d\n", result); }

return 0; }
6.OUTPUT :

For Binary Search:

For Interpolation Search:


7. TIME COMPLEXITY :

For Binary Search:

a. Best Case: O(1) - This occurs when the middle element is the target element.

b. Average Case: O(logn) - In each step, the search interval is divided by half.

c. Worst Case: O(logn) - The maximum number of steps needed to reduce the search interval
to 1 is logn.

For Interpolation Search:

1) Best Case:O(1) - This occurs when the target is exactly at the estimated position.

2) Average Case: O(log log n) - This is the expected time complexity for uniformly distributed
data.

3) Worst Case: O(n) - This occurs when the data is not uniformly distributed or is skewed.
8. WORKING OF THE EXAMPLE PROBLEM

For Binary Search:


For Interpolation Search:
9. REFERENCES

[1] https://www.javatpoint.com/daa-binary-search

[2] https://www.javatpoint.com/interpolation-search-in-
c#:~:text=Interpolation%20searches%20often%20perform%20(log,the%20total%20num
ber%20of%20elements.&text=Example%3A,(log(log%20n)).

[3] https://www.programiz.com/c-programming/online-compiler/

You might also like