You are on page 1of 6

EC-228 DSA

   LAB REPORT # 9

                   
Submitted by:
 Mahnoor Inam 
Amina Khalid
Reg. No: 
18-CE-009 
18-CE-017
Sunmitted to:
Sir Tauqeer
Date:
26-05-2020
Department of Computer Engineering
HITEC University Taxila
Experiment # 9
Implementation of Searching Algorithms in C++ programming language

Objectives:
The objective of this lab is to understand the basic concept of different searching
techniques
 Linear Search.
 Binary Search.
 Jump Search.
 Interpolation Search.

Theory:
The searching algorithms are used to search or find one or more than one element
from a dataset. These type of algorithms are used to find elements from a specific
data structures. Searching may be sequential or not. If the data in the dataset are
random, then we need to use sequential searching. Otherwise we can use other
different techniques to reduce the complexity. Search algorithms form an important
part of many programs. Some searches involve looking for an entry in a database,
such as looking up your record in the IRS database. Other search algorithms trawl
through a virtual space, such as those hunting for the best chess moves. Although
programmers can choose from numerous search types, they select the algorithm
that best matches the size and structure of the database to provide a user-friendly
experience. The general searching problem can be described as follows: Locate an
element x in a list of distinct elements a1,a2,...an or determine that it is not in the
list. The solution to this search problem is the location of the term in the list that
equals x and is 0 if x is not in the list.

Linear Search:
The linear search is the algorithm of choice for short lists, because it’s simple and
requires minimal code to implement. The linear search algorithm looks at the first
list item to see whether you are searching for it and, if so, you are finished. If not,
it looks at the next item and on through each entry in the list.
Binary Search:
Binary Search is one of the most fundamental and useful algorithms in Computer
Science. It describes the process of searching for a specific value in an ordered
collection.

Lab task:
Write C++ codes for the Jump or Interpolation search.

#include<bits/stdc++.h>
using namespace std;
int interpolationSearch(int arr[], int n, int x)
{
    int lo = 0, hi = (n - 1);
    while (lo <= hi && x >= arr[lo] && x <= arr[hi])
    {
        if (lo == hi)
        {
            if (arr[lo] == x) return lo;
            return -1;
        }
        int pos = lo + (((double)(hi - lo) /
            (arr[hi] - arr[lo])) * (x - arr[lo]));
        if (arr[pos] == x)
            return pos;
        if (arr[pos] < x)
            lo = pos + 1;
        else
            hi = pos - 1;
    }
    return -1;
}

int main()
{
    int arr[] = {10, 12, 13, 16, 18, 19, 20, 21,
                 22, 23, 24, 33, 35, 42, 47};
    int n = sizeof(arr)/sizeof(arr[0]);

    int x = 18; 
    int index = interpolationSearch(arr, n, x);

    if (index != -1)


        cout << "Element found at index " << index;
    else
        cout << "Element not found.";
    return 0;
}
Output:

Write a C++ program to implement binary search on an integer in a single


linked list.
   

#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct Node
{
    int data;
    struct Node* next;
};

Node *newNode(int x)
{
    struct Node* temp = new Node;
    temp->data = x;
    temp->next = NULL;
    return temp;
}
struct Node* middle(Node* start, Node* last)
{
    if (start == NULL)
        return NULL;

    struct Node* slow = start;


    struct Node* fast = start -> next;

    while (fast != last)


    {
        fast = fast -> next;
        if (fast != last)
        {
            slow = slow -> next;
            fast = fast -> next;
        }
    }

    return slow;
}
struct Node* binarySearch(Node *head, int value)
{
    struct Node* start = head;
    struct Node* last = NULL;

    do
    {
        Node* mid = middle(start, last);
        if (mid == NULL)
            return NULL;
        if (mid -> data == value)
            return mid;
        else if (mid -> data < value)
            start = mid -> next;
        else
            last = mid;

    } while (last == NULL ||


             last != start);
    return NULL;
}
int main()
{
    Node *head = newNode(1);
    head->next = newNode(4);
    head->next->next = newNode(7);
    head->next->next->next = newNode(8);
    head->next->next->next->next = newNode(9);
    head->next->next->next->next->next = newNode(10);
    int value = 7;
    if (binarySearch(head, value) == NULL)
        printf("Value not present\n");
    else
        printf("Present");
    return 0;
}

Output:

Conclusion:
In this lab we learned how to search the element by linear, binary, jump and
interpolation method.

You might also like