LAB 5: Search+H Ash+graph EX1:: Test Result
LAB 5: Search+H Ash+graph EX1:: Test Result
LAB 5: Search+H Ash+graph EX1:: Test Result
EX1:
Implement function
int binarySearch(int arr[], int left, int right, int x)
to search for value x in array arr using recursion.
After traverse an index in array, we print out this index using cout << "We traverse on index: " << index <<
endl;
Note that middle of left and right is floor((right-left)/2)
For example:
Test Result
EX2:
Given an array of distinct integers, find if there are two pairs (a, b) and (c, d) such that a+b = c+d,
and a, b, c and d are distinct elements. If there are multiple answers, you can find any of them.
Some libraries you can use in this question:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <set>
Note: The function checkAnswer is used to determine whether your pairs found is true or not in
case there are two pairs satistify the condition. You don't need to do anything about this function.
For example:
Test Result
EX3:
Implement function
int foldShift(long long key, int addressSize);
int rotation(long long key, int addressSize);
to hashing key using Fold shift or Rotation algorithm.
Review Fold shift:
The folding method for constructing hash functions begins by dividing the item into equal-size
pieces (the last piece may not be of equal size). These pieces are then added together to give the
resulting hash value.
For example:
Test Result
piece = stoi(strPiece);
sum += piece;
}
string strSum = to_string(sum);
size_t L1 = strSum.length();
string result = strSum.substr(L1-addressSize,addressSize);
return stoi(result);
}
int rotation(long long key, int addressSize)
{
string strKey = to_string(key);
size_t L = strKey.length();
string rotation = "";
rotation += strKey[L-1];
strKey = strKey.substr(0,L-1);
rotation += strKey;
return foldShift(stoll(rotation),addressSize);
}
EX4:
Implement function
After traverse to an index in array, before returning the index or passing it as argument to recursive
function, we print out this index using cout << "We traverse on index: " << index << endl;
Please note that you can't using key work for, while, goto (even in variable names, comment).
For example:
Test Result
EX5:
In computer science, a jump search or block search refers to a search algorithm for ordered lists. The basic idea
is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in
place of searching all elements. For example, suppose we have an array arr[] of size n and block (to be
jumped) size m. Then we search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the
interval (arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the
element x. The optimal value of m is √n, where n is the length of the list.
In this question, we need to implement function jumpSearch with step √n to search for value x in array arr.
After searching at an index, we should print that index until we find the index of value x in array or
until we determine that the value is not in the array.
}
Test Expected Got
if (index != -1) {
cout << "\nNumber " << x << " is at index " <<
index;
}
else {
cout << "\n" << x << " is not in array!";
}
if (index != -1) {
cout << "\nNumber " << x << " is at index " <<
index;
}
else {
cout << "\n" << x << " is not in array!";
}
if (index != -1) {
cout << "\nNumber " << x << " is at index " <<
index;
}
else {
cout << "\n" << x << " is not in array!";
}
if (index != -1) {
cout << "\nNumber " << x << " is at index " <<
index;
}
else {
cout << "\n" << x << " is not in array!";
}
Passed all tests!
EX6:
Implement three following hashing function:
long int midSquare(long int seed);
long int moduloDivision(long int seed, long int mod);
long int digitExtraction(long int seed, int* extractDigits, int size);
Note that:
In midSquare function: we eliminate 2 last digits and get the 4 next digits.
In digitExtraction: extractDigits is a sorted array from smallest to largest index of digit in seed (index starts
from 0). The array has size size.
For example:
Test Result