0% found this document useful (0 votes)
8 views3 pages

Recursive and Binary Search Programs

The document contains practical exercises for a Design and Analysis of Algorithm Lab, specifically focusing on recursive linear search and binary search algorithms. It provides C code implementations for both algorithms, including sample outputs demonstrating their functionality. The linear search program finds the index of a specified key in an array, while the binary search program does the same for a sorted array.

Uploaded by

roshni97777
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)
8 views3 pages

Recursive and Binary Search Programs

The document contains practical exercises for a Design and Analysis of Algorithm Lab, specifically focusing on recursive linear search and binary search algorithms. It provides C code implementations for both algorithms, including sample outputs demonstrating their functionality. The linear search program finds the index of a specified key in an array, while the binary search program does the same for a sorted array.

Uploaded by

roshni97777
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

INSTITUTE OF TECHNOLOGY & MANAGEMENT

Integrated Technical Campus: Engineering, Pharmacy & Management


Approved by AICTE, Pharmacy Council of India, New Delhi & Affiliated to Dr. APJAKTU,
AL-1, Sector - 7, GIDA, Gorakhpur - 273209 (UP)

Design and Analysis of Algorithm Lab (BCS553)

Practical-1

(a) Program for Recursive Linear Search.

#include <stdio.h>

int linearSearch(int arr[], int size, int key)

// If the size of the array is zero, return -1

if (size == 0) {

return -1;

if (arr[size - 1] == key) {

return size - 1;

return linearSearch(arr, size - 1, key);

int main()

int arr[] = { 5, 15, 6, 9, 4 };

int key = 4;

int index

= linearSearch(arr, sizeof(arr) / sizeof(int), key);

if (index == -1) {

printf("Key not found in the array.\n");


INSTITUTE OF TECHNOLOGY & MANAGEMENT
Integrated Technical Campus: Engineering, Pharmacy & Management
Approved by AICTE, Pharmacy Council of India, New Delhi & Affiliated to Dr. APJAKTU,
AL-1, Sector - 7, GIDA, Gorakhpur - 273209 (UP)

else {

printf("The element %d is found at %d index of the "

"given array \n",

key, index);

return 0;

➢ Output

The element 4 is found at 4 index of the given array.

(b) Program for Binary Search: -

#include <stdio.h>
int binarySearch(int arr[], int low, int high, int x)
{
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;
}
INSTITUTE OF TECHNOLOGY & MANAGEMENT
Integrated Technical Campus: Engineering, Pharmacy & Management
Approved by AICTE, Pharmacy Council of India, New Delhi & Affiliated to Dr. APJAKTU,
AL-1, Sector - 7, GIDA, Gorakhpur - 273209 (UP)

return -1;
}

int main(void)

{
int arr[] = { 2, 3, 4, 10, 40 };
int n = size of(arr) / size of (arr[0]);
int x = 10;
int result = binarySearch (arr, 0, n - 1, x);
if (result == -1) printf ("Element is not present in array");
else printf ("Element is present at index %d",result);

➢ Output: -

Element is present at index 3

Common questions

Powered by AI

Divide-and-conquer algorithms like binary search efficiently solve problems by breaking them into smaller subproblems, solving these recursively, and merging their results. This approach significantly reduces complexity, as seen by binary search dividing and half-reducing its search space iteratively, making it more efficient for large datasets.

Recursive implementations, like recursive linear search, use call stack memory proportional to the array size (O(n) in the worst case), leading to possible stack overflow in large arrays. Iterative versions avoid this by maintaining a single frame on the stack, thus favoring memory efficiency. However, recursion can offer clearer, more concise code that may enhance readability and allow natural implementation of divide-and-conquer strategies.

The base case in the recursive linear search occurs when the size of the array becomes zero (meaning the element is not found) or when the element at the current index matches the key. This prevents infinite recursion by providing a condition for terminating the recursive calls, ensuring the function ends with either finding the element or confirming its absence.

To find all occurrences of a given key in a list with duplicates using binary search, first locate any occurrence of the key, then expand the search to both left and right of this index to gather all indices with the same value. This can involve modified binary search operations to locate the first and last occurrence by adjusting the mid-point checks.

In binary search, the mid-point is calculated as 'low + (high - low) / 2'. This method helps avoid potential overflow issues with large index values and efficiently narrows down the search range by splitting the array into two halves, allowing the algorithm to discard one half, hence reducing the search space logarithmically.

Linear search has a time complexity of O(n) as it checks each element sequentially, suitable for small or unsorted data sets. Binary search has a time complexity of O(log n) but requires the array to be sorted before use. Binary search is more efficient for large sorted datasets due to its divide-and-conquer approach, whereas linear search is simpler and more versatile for unsorted arrays.

A recursive linear search function checks each element of the array starting from the last index towards the first. If it finds the key at the current index, it returns the index; otherwise, it invokes itself to check the preceding elements. Its time complexity is O(n) because in the worst case, it has to check each element in the array sequentially.

Binary search requires the array to be sorted beforehand, which can be a limitation if the array elements change frequently, requiring repeated sorts. The overhead of sorting can negate the benefits of binary search for small data sets. Additionally, binary search is inefficient for data structures that do not support random access, such as linked lists.

Linear search is preferable when dealing with small or unsorted arrays due to its simplicity and flexibility. It is also useful when the cost of sorting the data (for binary search applicability) outweighs the benefits, such as in real-time systems where data is frequently updated or when a search function needs to be implemented quickly with minimal overhead.

The initial parameters, specifically the array size and search key, dictate the start and conditions of the recursive process. A miscalculated size may cause missed elements and a wrong key will lead inevitably to a '-1' outcome indicating failure. Correct parameterization ensures the search function performs the expected checks efficiently.

You might also like