I'm trying to implement linear probing. I want to know how negative values are handled in this technique. In the below code, I have written a function for positive values. Also, what if -1 was an element in the array? How are we going to handle it?
int[] linearProbing(int hash_size, int arr[], int sizeOfArray)
{
//Your code here
int []table = new int[hash_size];
for(int i=0;i<hash_size;i++){
table[i] = -1;
}
int key = 0;
for(int i=0;i<sizeOfArray;i++){
key = arr[i] % hash_size;
if(table[key] == arr[i]){
continue;
}
if(table[key] == -1){
table[key] = arr[i];
}
else{
int k = 1;
int j = (key+k) % hash_size;
while(table[j] != -1){
if(j == key){
return table;
}
if(table[j] == arr[i]){
break;
}
else{
k++;
j = (key+k) % hash_size;
}
}
table[j] = arr[i];
}
}
return table;
}