Thursday 15 August 2013

hash funcion in C -



hash funcion in C -

i need create hashing function... can u help me ?

the input sequence of numbers.your task determine number of how many numbers repeated.

its string of numbers , letters (*a[]). n number of digits -input.

returns number of repetition.

int function(char *a[], int n) { int i,j; int same=0; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(!strcmp(a[i],a[j])) same++; } } homecoming same; } int main(void) { char *a[] = {"aa123456", "ba987689", "aa123123", "aa312312", "bb345345", "aa123123"}; printf("number of duplicates: %d\n", function(a, 6)); homecoming 0; }

read wikipage on hash functions & hash tables.

often, linear combination prime coefficients (see bézout's identity) , involving components , partial hash gives plenty result.

for example, like

int basile_hash(const char*str) { int h = 65537; while (*str) { h = 75553*h + 5531* (*str); str++; }; homecoming h; }

i don't claim hash, plenty needs. constants 65537, 75553, 5531 primes (given /usr/games/primes bsdgames debian package)

you create variant bitwise xor ^, or take business relationship more 1 component:

h = 65579*str[0] ^ 5507*str[1] + 17*h; str += 2;

but should care -and special-case when s[1] terminating null byte.

read md5

notice lot of standard or popular libraries gives many hash functions. of time particular selection of hash function not important. on other hand, can still earn phd on studying , inventing hash functions. have 1 in values.c file, function mom_cstring_hash near line 150 (i imagine might improve optimized, since big strings of instructions might run "in parallel" within processor).

i don't claim expert on hash functions.

study source code of hash functions in free software libraries glib, qt, etc.... see gperf

c hash

No comments:

Post a Comment