I made some algorithms for sorting and searchimg. I've read that hashing can be efficient for searching (without comparing). I don't know how to make hashtable, insert and search for element. I searched this board and found interesting stuff but pretty
much complicated.
I wrote simple function that implement binary search (just for demonstartion of how it's works). I decide to implement hash function key%capacity.My key is of course element in array. But I don't know can I search element through table and collision resolve using open adressing. I really don't
understand this completely without code. So if you could give me some example code (with open adressing because it is simple) to fit in with these simple functions?
;Code:#include <iostream> using namespace std; const int capacity=5;//capacity of the hash table void bubble_sort(int array[],int length) { int swapflag=1; for(int i=0;i<(length-1) && swapflag;i++) { swapflag=0; for(int j=0;j<(length-(i+1));j++) if(array[j]>array[j+1]) { swap(array[j],array[j+1]); swapflag=1; } } } int* binary_search(int array[],int value,int length) { int head=0,end=length-1,middle; bubble_sort(array,length); while(head<=end) { middle=(head+end)/2; if(array[middle]==value) return &array[middle]; else if(value>array[middle]) head=middle+1; else end=middle-1; } return 0; } //and with hashing...? int hash_function(int key) { return key%capacity; } int main ( ) { int array[]={10,8,9,5,6,7,3,2,1,4}; int* x; x=binary_search(array,5,10); if(!x) { cout<<"element doesn't exist"; return 0; } cout<<*x; return 0; }
Thanks



LinkBack URL
About LinkBacks



