Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to turn C to Java. Thank you and will rate. -------------------------------

ID: 3780371 • Letter: N

Question

Need to turn C to Java.

Thank you and will rate.

--------------------------------------------

djb2

this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.

------------------------------------------

sdbm

this algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. it was found to do well in scrambling bits, causing better distribution of the keys and fewer splits. it also happens to be a good general hashing function with good distribution. the actual function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below is the faster version used in gawk. [there is even a faster, duff-device version] the magic constant 65599 was picked out of thin air while experimenting with different constants, and turns out to be a prime. this is one of the algorithms used in berkeley db (see This is not the best possible algorithm, but it has the merit of extreme simplicity." This is an understatement; It is a terrible hashing algorithm, and it could have been much better without sacrificing its "extreme simplicity." [see the second edition!] Many C programmers use this function without actually testing it, or checking something like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise respectable code, eg. cnews. sigh. [see also: tpop]

---------------

lose lose

This hash function appeared in K&R (1st ed) but at least the reader was warned: "This is not the best possible algorithm, but it has the merit of extreme simplicity." This is an understatement; It is a terrible hashing algorithm, and it could have been much better without sacrificing its "extreme simplicity." [see the second edition!] Many C programmers use this function without actually testing it, or checking something like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise respectable code, eg. cnews. sigh. [see also: tpop]

Explanation / Answer

package demo;

public class DemoTranslation {
   public static long hash_U(byte[] str_U) {
       int strIndex = 0;
       long hash_U = 5_381;
       int c;

       while((c = Byte.toUnsignedInt(str_U[strIndex++])) != 0) {
           hash_U = ((hash_U << 5) + hash_U) + c; /* hash * 33 + c */
       }

       return hash_U;
   }
}

package demo;

public class DemoTranslation {
   public static long hash_U(byte[] str_U) {
       int strIndex = 0;
       int hash_U = 0;
       int c;

       while((c = Byte.toUnsignedInt(str_U[strIndex++])) != 0) {
           hash_U += c;
       }

       return Integer.toUnsignedLong(hash_U);
   }
}

package demo;

public class DemoTranslation {
   public static long hash_U(byte[] str_U) {
       int strIndex = 0;
       int hash_U = 0;
       int c;

       while((c = Byte.toUnsignedInt(str_U[strIndex++])) != 0) {
           hash_U += c;
       }

       return Integer.toUnsignedLong(hash_U);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote