Program 01
Linear search
Write a program in c to search an element using linear search and find its execution time using the time function.
Algorithm
Algorithm: LinearSearch(A, n, x) 1. Start 2. For i <- 0 to (n-1) do 3. if x == A[i] then 4. print "Element Found" 5. exit 6. If (i==n) then 7. print "Element Not Found" 8. Stop
Program
#include<stdio.h>
#include<time.h>
int main() {
int n,i,x,count=0;
printf("Enter the number of elements:");
scanf("%d",&n);
int a[n];
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to search:");
scanf("%d",&x);
clock_t start=clock();
for(i=0;i<n;i++) {
count++;
if(x==a[i])
break;
}
clock_t end=clock();
double time_taken=(double)(end-start)/CLOCKS_PER_SEC;
if(i==n)
printf("\n%d is not found",x);
else
printf("\n%d is found at position %d",x,i+1);
printf("\nNumber of comparisons:%d",count);
printf("\nTime taken:%f seconds",time_taken);
printf("\nTime complexity: best case O(1), worst case O(n)");
printf("\nSpace complexity: O(1)");
printf("\n");
return 0;
}Output
Input typed: 6 ⏎ 14 27 8 35 19 42 ⏎ 35
Enter the number of elements:6 Enter the elements:14 27 8 35 19 42 Enter the element to search:35 35 is found at position 4 Number of comparisons:4 Time taken:0.000003 seconds Time complexity: best case O(1), worst case O(n) Space complexity: O(1)
Complexity
Time complexity: best case O(1), worst case O(n) Space complexity: O(1)
| Best case | O(1) |
|---|---|
| Average case | O(n) |
| Worst case | O(n) |
| Paradigm | Brute force |
Viva: which header is used and why
Q. Why #include <stdio.h>?
A. stdio.h is standard input/output: printf, scanf. Used here for: printf, scanf.
Q. Why #include <time.h>?
A. time.h: clock(), clock_t, CLOCKS_PER_SEC for timing. Used here for: clock, clock_t, CLOCKS_PER_SEC.
Viva: what does each line do?
int main() {This line defines a function. main() is where the program starts running.
int n,i,x,count=0;
Declares the variables used below. n is the number of elements (or processes). count tallies the basic operations (comparisons/steps) to show the complexity.
printf("Enter the number of elements:");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
scanf("%d",&n);This line reads input. scanf() reads input into variables; pass their addresses with &. n is the number of elements (or processes).
int a[n];
Declares the variables used below. n is the number of elements (or processes).
printf("Enter the elements:");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
for(i=0;i<n;i++)
This line is the loop header. for (init; condition; update) repeats a block a counted number of times. n is the number of elements (or processes).
scanf("%d",&a[i]);This line reads input. scanf() reads input into variables; pass their addresses with &.
printf("Enter the element to search:");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
scanf("%d",&x);This line reads input. scanf() reads input into variables; pass their addresses with &.
clock_t start=clock();
This line records the time. clock_t is the type that holds clock() readings. start stores clock() before the algorithm runs.
for(i=0;i<n;i++) {This line is the loop header. for (init; condition; update) repeats a block a counted number of times. n is the number of elements (or processes).
count++;
Counts one more basic operation. count tallies the basic operations (comparisons/steps) to show the complexity.
if(x==a[i])
This line is a decision. if runs the block only when the condition is true.
break;
break leaves the nearest loop (or switch) immediately.
clock_t end=clock();
This line records the time. clock_t is the type that holds clock() readings. end stores clock() after the algorithm finishes.
double time_taken=(double)(end-start)/CLOCKS_PER_SEC;
time_taken = (end − start) / CLOCKS_PER_SEC, in seconds. end stores clock() after the algorithm finishes. start stores clock() before the algorithm runs.
if(i==n)
This line is a decision. if runs the block only when the condition is true. n is the number of elements (or processes).
printf("\n%d is not found",x);This line prints output. printf() prints formatted output (%d int, %f float, %s string).
printf("\n%d is found at position %d",x,i+1);This line prints output. printf() prints formatted output (%d int, %f float, %s string).
printf("\nNumber of comparisons:%d",count);This line prints output. printf() prints formatted output (%d int, %f float, %s string). count tallies the basic operations (comparisons/steps) to show the complexity.
printf("\nTime taken:%f seconds",time_taken);This line prints output. printf() prints formatted output (%d int, %f float, %s string). time_taken = (end − start) / CLOCKS_PER_SEC, in seconds.
printf("\nTime complexity: best case O(1), worst case O(n)");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
printf("\nSpace complexity: O(1)");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
printf("\n");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
return 0;
This line returns from the function. return exits the function and hands back a value.
Viva: concepts
Q. What is the time complexity of Linear search?
A. Best O(1), average O(n), worst O(n). Design paradigm: Brute force.
Q. Linear search's best case happens when…
A. The key is the first element. One comparison, so O(1).
Q. Does linear search need a sorted array?
A. No, it works on any order. Binary search is the one that needs sorted input.