0% found this document useful (0 votes)
28 views

123 Qs

The document describes 3 algorithms for searching an array for a key element: 1. A linear search algorithm that searches the entire array sequentially in O(n) time. 2. A binary search algorithm that searches a sorted array in O(logn) time. 3. An algorithm that searches a sorted array by first checking elements at indexes that are powers of 2 (arr[0], arr[2], arr[4] etc.), then performing a linear search between those indexes if a match is found, resulting in sub-linear search time.

Uploaded by

Chetan Lunthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

123 Qs

The document describes 3 algorithms for searching an array for a key element: 1. A linear search algorithm that searches the entire array sequentially in O(n) time. 2. A binary search algorithm that searches a sorted array in O(logn) time. 3. An algorithm that searches a sorted array by first checking elements at indexes that are powers of 2 (arr[0], arr[2], arr[4] etc.), then performing a linear search between those indexes if a match is found, resulting in sub-linear search time.

Uploaded by

Chetan Lunthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SOURCE CODE

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)

#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 bubblesort(int ar[], int n)


{
   
    for(int i=0; i < n-1; i++)
    {
      for(int j=0; j < n-i; j++)
      {
        if(ar[j] > ar[j+1])
        {
            int temp;
            temp = ar[j];
            ar[j] = ar[j+1];
            ar[j+1] = temp;
        }
        }
    }
   
    printf("\nThe new sorted array using bubble sort is: \n");
    for(int i=0; i<n; i++)
    {
        printf("%d\t",ar[i]);
    }
    printf("\n");
}

int BinarySearch(int ar[], int n, int key)

{
   
    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);

    printf("\nTime taken to sort the array is: %lf microseconds\n", time);


    if(result == -1)
    {
        printf("\nElement not found \n");
    }
    else
    {
        printf("Element found\n");
    }
    return 0;
}
OUTPUT
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)

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;
}

int JP(int arr[],int n,int k,int* C)


{
    int i=1,j;
    while(i<=n)
    {
        if(k<=arr[i])
        {
            break;
        }
        i=i*2;
    }
    for(j=i/2;j<=i;j++)
    {
        (*C)++;
        if(arr[j]==k)
        {
            return 1;
            break;
        }
    }
    return 0;
}
OUTPUT
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):

You might also like