forked from anurag1802/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadraticprob.cpp
58 lines (51 loc) · 1.53 KB
/
quadraticprob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*quadratic probing*/
#include<iostream>
#define dataSize 10 /*total number of data*/
#define hashTableSize 10 /*size of hashtable*/
using namespace std;
/*hash function to locate data*/
int hashFunction(int k)
{
return (2*k+3)%hashTableSize;
}
/* main function */
int main()
{
int data[dataSize]={3,2,9,6,11,13,7,12,70,80};
int hashTable[hashTableSize],i,position,probes[hashTableSize],tempPosition,counter,elementsInHashTable=1;
/*set -1 to indicate that cell is empty*/
for(i=0;i<hashTableSize;++i)hashTable[i]=-1;
/*process each data one by one*/
for(i=0;i<dataSize;++i)
{
cout<<"i="<<i;
cout<<"\tdata["<<i<<"]="<<data[i];
position=hashFunction(data[i]);
cout<<"\tposition given by function ="<<position;
counter=1;
while(hashTable[tempPosition]!=-1)
{
tempPosition=position+(counter*counter);
tempPosition=tempPosition%hashTableSize;
counter++;
}
position=tempPosition;
cout<<"\tposition incremented after collision ="<<position;
hashTable[position]=data[i];
cout<<"\thashTable position ="<<hashTable[position]<<"\n";
probes[position]=counter;
elementsInHashTable++;
if(elementsInHashTable>hashTableSize)
{
cout<<"\nhash table is full and hence can not process further "<<dataSize-hashTableSize<<" more data\n";
break;
}
}
/*display*/
cout<<"status of hash table after mapping the data onto it\n";
for(i=0;i<hashTableSize;++i)
{
cout<<"hashTable["<<i<<"]="<<hashTable[i]<<"=> Number of probes= "<<probes[i]<<endl;
}
return 0;
}