0% found this document useful (0 votes)
14 views11 pages

Notes On Search

Search technique notes

Uploaded by

Prabat Raghav
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)
14 views11 pages

Notes On Search

Search technique notes

Uploaded by

Prabat Raghav
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/ 11

[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

Linear Search

Linear search is a very simple search algorithm. In this type of search, a sequential search is made over
all items one by one. Every item is checked and if a match is found then that particular item is returned,
otherwise the search continues till the end of the data collection.

Algorithm
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Pseudocode
procedure linear_search (list, value)

for each item in the list


if match item == value
return the item's location
end if
end for

end procedure
Here we present the implementation of linear search in C programming language. The output of the
program is given after the code.

Linear Search Program


#include <stdio.h>
#define MAX 20
// array of items on which linear search will be conducted.
int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};

void printline(int count) {


int i;

for(i = 0;i <count-1;i++) {


printf("=");
}

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

printf("=\n");
}

// this method makes a linear search.


int find(int data) {

int comparisons = 0;
int index = -1;
int i;

// navigate through all items


for(i = 0;i<MAX;i++) {
// count the comparisons made
comparisons++;
// if data found, break the loop
if(data == intArray[i]) {
index = i;
break;
}
}
printf("Total comparisons made: %d", comparisons);
return index;
}

void display() {
int i;
printf("[");

// navigate through all items


for(i = 0;i<MAX;i++) {
printf("%d ",intArray[i]);
}

printf("]\n");
}

void main() {
printf("Input Array: ");
display();
printline(50);

//find location of 1
int location = find(55);

// if element was found


if(location != -1)
printf("\nElement found at location: %d" ,(location+1));
else
printf("Element not found.");
}

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

If we compile and run the above program, it will produce the following result −

Output
Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]
==================================================
Total comparisons made: 19
Element found at location: 19
-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8

Binary search
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm
works on the principle of divide and conquer. For this algorithm to work properly, the data collection
should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the collection. If a match
occurs, then the index of item is returned. If the middle item is greater than the item, then the item is
searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-
array to the right of the middle item. This process continues on the sub-array as well until the size of the
subarray reduces to zero.

How Binary Search Works?


For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the process
of binary search with a pictorial example. The following is our sorted array and let us assume that we
need to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula −


mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the
value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted
array, so we also know that the target value must be in the upper portion of the array.

We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value
must be in the lower part from this location.

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We find that it is a match.

We conclude that the target value 31 is stored at location 5.


Binary search halves the searchable items and thus reduces the count of comparisons to be made to very
less numbers.

Pseudocode
The pseudocode of binary search algorithms should look like this −
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched

Set lowerBound = 1
Set upperBound = n

while x not found


if upperBound < lowerBound
EXIT: x does not exists.

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2

if A[midPoint] < x
set lowerBound = midPoint + 1

if A[midPoint] > x
set upperBound = midPoint - 1

if A[midPoint] = x
EXIT: x found at location midPoint
end while

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

end procedure
Interpolation search is an improved variant of binary search. This search algorithm works on the probing
position of the required value. For this algorithm to work properly, the data collection should be in a
sorted form and equally distributed.
Binary search has a huge advantage of time complexity over linear search. Linear search has worst-case
complexity of Ο(n) whereas binary search has Ο(log n).
There are cases where the location of target data may be known in advance. For example, in case of a
telephone directory, if we want to search the telephone number of Morphius. Here, linear search and
even binary search will seem slow as we can directly jump to memory space where the names start from
'M' are stored.

Positioning in Binary Search


In binary search, if the desired data is not found then the rest of the list is divided in two parts, lower and
higher. The search is carried out in either of them.

Even when the data is sorted, binary search does not take advantage to probe the position of the
desired data.

Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going
to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and
the following items are to be stored. Item are in the (key, value) format.

 (1,20)
 (2,70)
 (42,80)
 (4,25)
 (12,44)
 (14,32)
 (17,11)
 (13,78)
 (37,98)
Sr.No. Key Hash Array Index

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17

Linear Probing
As we can see, it may happen that the hashing technique is used to create an already used index of the
array. In such a case, we can search the next empty location in the array by looking into the next cell
until we find an empty cell. This technique is called linear probing.
Sr.No. Key Hash Array Index After Linear Probing, Array Index
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18

Basic Operations
Following are the basic primary operations of a hash table.
 Search − Searches an element in a hash table.
 Insert − inserts an element in a hash table.
 delete − Deletes an element from a hash table.

DataItem
Define a data item having some data and key, based on which the search is to be conducted in a hash
table.
struct DataItem {
int data;
int key;
};

Hash Method
Define a hashing method to compute the hash code of the key of the data item.
int hashCode(int key){
return key % SIZE;
}

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

Search Operation
Whenever an element is to be searched, compute the hash code of the key passed and locate the element
using that hash code as index in the array. Use linear probing to get the element ahead if the element is
not found at the computed hash code.

Example
struct DataItem *search(int key) {
//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

Insert Operation
Whenever an element is to be inserted, compute the hash code of the key passed and locate the index
using that hash code as an index in the array. Use linear probing for empty location, if an element is
found at the computed hash code.

Example
void insert(int key,int data) {
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

hashArray[hashIndex] = item;
}

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

Delete Operation
Whenever an element is to be deleted, compute the hash code of the key passed and locate the index
using that hash code as an index in the array. Use linear probing to get the element ahead if an element is
not found at the computed hash code. When found, store a dummy item there to keep the performance of
the hash table intact.

Example
struct DataItem* delete(struct DataItem* item) {
int key = item->key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] !=NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];

//assign a dummy item at deleted position


hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}
Hash Table is a data structure which stores data in an associative manner. In hash table, the data is stored
in an array format where each data value has its own unique index value. Access of data becomes very
fast, if we know the index of the desired data.

Implementation in C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 20

struct DataItem {
int data;
int key;
};

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

struct DataItem* hashArray[SIZE];


struct DataItem* dummyItem;
struct DataItem* item;

int hashCode(int key) {


return key % SIZE;
}

struct DataItem *search(int key) {


//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

void insert(int key,int data) {

struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));


item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

hashArray[hashIndex] = item;
}

struct DataItem* delete(struct DataItem* item) {


int key = item->key;

//get the hash


int hashIndex = hashCode(key);

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];

//assign a dummy item at deleted position


hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

void display() {
int i = 0;

for(i = 0; i<SIZE; i++) {

if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}

printf("\n");
}

int main() {
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = -1;
dummyItem->key = -1;

insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);

display();
item = search(37);

By: Prabhat Raghav


[ DATA STRUCTURE USING ‘C’ AND ‘C++’]

if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}

delete(item);
item = search(37);

if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
}
If we compile and run the above program, it will produce the following result −

Output
~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44) (13,78) (14,32) ~~ ~~ (17,11)
(37,97) ~~
Element found: 97
Element not found

By: Prabhat Raghav

You might also like