123 Qs
123 Qs
#include<stdio.h>
#include<time.h>
int search(int ar[], int n, int key)
{
int flag = 0;
for(int i = 0; i<n ; i++)
{
if(ar[i] == key)
{
flag++;
break;
}
else
{
continue;
}
}
return flag;
}
int main()
{
int ar[100000], n, key;
printf("Enter the number of elements you want: ");
scanf("%d",&n);
int a[n];
for(int i=0; i<n; i++)
{
a[i]= rand() % (n*10)+1;
printf("%d\t",a[i]);
}
printf("\nEnter the number you want to search: ");
scanf("%d",&key);
clock_t start, end;
start = clock();
int result = search(a, n, key);
end = clock();
double time = (double)(end - start)/CLOCKS_PER_SEC;
printf("Time required = %lf seconds.\n",time);
if(result == 0)
printf("element not found\n");
else
printf("Element found successfully\n");
return 0;
}
OUTPUT
Q1. Given an array of nonnegative integers, design a linear algorithm and
implement it using a program to find whether given key element is present
in the array or not. Also, find total number of comparisons for each input
case. (Time Complexity = O(n), where n is the size of input)
GRAPH Q1
0.000016
0.000014
0.000012
0.00001
TIME
0.000008
0.000006
0.000004
0.000002
0
1000 2000 3000 4000
NO OF ITEMS
SOURCE CODE
Q2. Given an already sorted array of positive integers, design an algorithm
and implement it using a program to find whether given key element is
present in the array or not. Also, find total number of comparisons for each
input case. (Time Complexity = O(logn), where n is the size of input)
#include<stdio.h>
#include<time.h>
{
int low =0, mid, high = n-1;
while(low < high)
{
mid = (low +high)/2;
if(key == ar[mid])
{
return mid;
}
else if(key < ar[mid])
{
high = mid - 1;
}
else if(key > ar[mid])
{
low = mid + 1;
}
else
{
return -1;
}
}
}
int main()
{
clock_t start, end;
int n, key;
printf("Enter number of elements you want to enter : ");
scanf("%d",&n);
int ar[n];
for(int i=0; i<n; i++)
{
ar[i] = rand()%(n*10)+1;
printf("%d\t", ar[i]);
}
bubblesort(ar,n);
printf("\nEnter the number you want to search: ");
scanf("%d",&key);
start = clock();
int result = BinarySearch(ar, n, key);
end = clock();
double time = (double)(end - start) / (CLOCKS_PER_SEC);
GRAPH Q2
0.00035
0.0003
0.00025
0.0002
TIME
0.00015
0.0001
0.00005
0
1000 2000 3000 4000
NO OF ITEMS
SOURCE CODE
Q3. Given an already sorted array of positive integers, design an algorithm
and implement it using a program to find whether a given key element is
present in the sorted array or not. For an array arr[n], search at the
indexes arr[0], arr[2], arr[4],. ,arr[2k ] and so on. Once the interval (arr[2k
] < key < arr[ 2k+1] ) is found, perform a linear search operation from the
index 2k to find the element key. (Complexity < O(n), where n is the
number of elements need to be scanned for searching):
#include <stdio.h>
int JP(int [],int,int,int *);
int main(void)
{
int t,n,i,k,C=0;
printf("Enter The Total Number of Test cases: ");
scanf("%d",&t);
while(t>0)
{
t--;
printf("Enter The Size of The Array: ");
scanf("%d",&n);
int arr[n];
printf("Enter The Elements of Array: ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter The Key Element: ");
scanf("%d",&k);
if(JP(arr,n,k,&C))
{
printf("Present : %d \n",C);
}
else
{
printf("Not present: %d \n",C);
}
}
return 0;
}