Posts

Showing posts with the label cost

THE DESIGN AND ANALYSIS OF ALGORITHMS

 THE DESIGN AND ANALYSIS OF ALGORITHMS LAB PROGRAMS Program 1. Write a program to implement linear search algorithm Repeat the experiment for different values of n, the number of elements in the list to be searched and plot a graph of the time taken versus n. #include <stdio.h> #include <stdlib.h> #include <time.h> // Function prototypes int linearSearch( int arr[], int n, int key) ; void generateRandomArray( int arr[], int n) ; int main() {     int arraySizes[] = { 100 , 500 , 1000 , 5000 , 10000 }; // Different values of n     clock_t start, end;     double cpu_time_used;     // Perform experiment for each array size     for ( int i = 0 ; i < sizeof(arraySizes) / sizeof(arraySizes[ 0 ]); i ++ ) {         int n = arraySizes[i];         int arr[n];         generateRandomArray(arr, n); // Generate a random array of size n   ...