Given a limited range array contains both positive and non positive numbers, i.e., elements are in range from -MAX to +MAX. Our task is to search for some number is present in the array of not in O(1) time.
Since range is limited, we can use index mapping (or trivial hashing). We use values as index in a big array. Therefore we can search and insert elements in O(1) time.
How to handle negative numbers?
The idea is to use a 2D array of size hash[MAX+1][2]
Algorithm:
Assign all the values of the hash matrix as 0.
Traverse the given array:
If the element ele is non negative assign
hash[ele][0] as 1.
Else take the absolute value of ele and
assign hash[ele][1] as 1.
To search any element x in the array.
If X is non-negative check if hash[X][0] is 1 or not. If hash[X][0] is one then the number is present else not present.
If X is negative take absolute vale of X and then check if hash[X][1] is 1 or not. If hash[X][1] is one then the number is present
C++
// CPP program to implement direct index mapping
// with negative values allowed.
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
// Since array is global, it is initialized as 0.
bool has[MAX + 1][2];
// searching if X is Present in the given array
// or not.
bool search(int X)
{
if (X >= 0) {
if (has[X][0] == 1)
return true;
else
return false;
}
// if X is negative take the absolute
// value of X.
X = abs(X);
if (has[X][1] == 1)
return true;
return false;
}
void insert(int a[], int n)
{
for (int i = 0; i < n; i++) {
if (a[i] >= 0)
has[a[i]][0] = 1;
else
has[abs(a[i])][1] = 1;
}
}
// Driver code
int main()
{
int a[] = { 2,-4,6,1,67,-98,100,23,-23};
int n = sizeof(a)/sizeof(a[0]);
cout<<sizeof(a)<<endl;
insert(a,n);
int X = -5;
if (search(X) == true)
cout << "Present";
else
cout << "Not Present";
return 0;
}
Java
import java.io.*;
import java.lang.Math;
class GFG {
static boolean[][] has = new boolean[1000][2];
public static boolean search(int x){
if(x>=0){
if(has[x][0]==true){
return true;
}else{
return false;
}
}
//absolute negative num;
x = Math.abs(x);
if(has[x][1]==true){
return true;
}else{
return false;
}
}
public static void insert(int[] arr){
int n = arr.length;
for(int i =0;i<n;i++){
if(arr[i]>=0){
has[arr[i]][0]=true;
}else{
has[Math.abs(arr[i])][1]=true;
}
}
}
public static void main (String[] args) {
System.out.println("GfG!");
int[] arr={2,-4,6,1,67,-98,100,23,-23};
int x = 23;
insert(arr);
if(search(x)==true){
System.out.println("Present!");
}else{
System.out.println("No Present!");
}
}
}
http://www.geeksforgeeks.org/index-mapping-or-trivial-hashing-with-negatives-allowed/
本文介绍了一种在包含正负数的有限范围数组中进行高效搜索的方法。通过使用二维哈希表,可以实现常数时间复杂度的插入和查找操作。
6万+

被折叠的 条评论
为什么被折叠?



