Knowee
Questions
Features
Study Tools

Write a single C program to implement Linear and binary search in two different functions. Plot the execution_time/input_size plot for both these functions for at least 10 input instances.

Question

Write a single C program to implement Linear and binary search in two different functions. Plot the execution_time/input_size plot for both these functions for at least 10 input instances.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that implements both linear and binary search.

#include <stdio.h>
#include <time.h>

// Linear Search Function
int linearSearch(int array[], int size, int element){
    for(int i=0; i<size; i++){
        if(array[i] == element){
            return i;
        }
    }
    return -1;
}

// Binary Search Function
int binarySearch(int array[], int size, int element){
    int low, mid, high;
    low = 0;
    high = size-1;
    // Keep searching until low <= high
    while(low<=high){
        mid = (low + high)/2;
        if(array[mid] == element){
            return mid;
        }
        if(array[mid]<element){
            low = mid+1;
        }
        else{
            high = mid-1;
        }
    } 
    return -1;
}

int main(){
    // Use the clock function to get the start time
    clock_t start, end;
    double cpu_time_used;
    
    int array[] = {1,2,3,4,5,6,7,8,9,10};
    int size = sizeof(array)/sizeof(int);
    
    // Linear Search
    start = clock();
    printf("Found element at index: %d\n", linearSearch(array, size, 5));
    end = clock();
    cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    printf("Time taken for linear search: %f\n", cpu_time_used);
    
    // Binary Search
    start = clock();
    printf("Found element at index: %d\n", binarySearch(array, size, 5));
    end = clock();
    cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    printf("Time taken for binary search: %f\n", cpu_time_used);
    
    return 0;
}

This program will print the index of the element if it is found, and -1 if it is not found. It will also print the time taken for each search.

To plot the execution time/input size plot, you would need to run this program with different input sizes and record the time taken for each. You can then use a tool like Excel or a library like matplotlib in Python to create the plot.

Please note that the time taken by these functions will be very small for small input sizes, and may even be reported as 0 by the clock function. You may need to use larger input sizes to see a difference. Also, the binary search function assumes that the input array is sorted.

This problem has been solved

Similar Questions

Program to implement linear search.

Write a C program to Search a Key string using Binary search TechniqueWrite a program to search a key string in the given array of strings using binary search.At the time of execution, the program should print the message on the console as:Enter value of n : For example, if the user gives the input as:Enter value of n : 4Next, the program should print the messages one by one on the console as:Enter string for a[0] : Enter string for a[1] : Enter string for a[2] : Enter string for a[3] : if the user gives the input as:Enter string for a[0] : AppleEnter string for a[1] : OrangeEnter string for a[2] : KiwiEnter string for a[3] : MangoNext, the program should print the message on the console as:Enter key string : if the user gives the input as:Enter key string : Kiwithen the program should print the result as:After sorting the strings in the array areValue of a[0] = AppleValue of a[1] = KiwiValue of a[2] = MangoValue of a[3] = OrangeThe key string Mango is found at the position 2Similarly if the key element is given as Litchi for the above case then the program should print the output as "The key string Litchi is not found in the array".Fill in the missing code so that it produces the desired result.Sample Test CasesTest Case 1:Expected Output:Enter·value·of·n·:·4Enter·string·for·a[0]·:·AppleEnter·string·for·a[1]·:·BananaEnter·string·for·a[2]·:·OrangeEnter·string·for·a[3]·:·MangoEnter·key·string·:·MangoAfter·sorting·the·strings·in·the·array·areValue·of·a[0]·=·AppleValue·of·a[1]·=·BananaValue·of·a[2]·=·MangoValue·of·a[3]·=·OrangeThe·key·string·Mango·is·found·at·the·position·2

4. Inbuilt binary search functions in C++

Write a function that performs binary search on a sorted array.

Write a program to read and store n+ve numbers in array and search a number k provided by the user using a. Linear Searchb. Binary Search

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.