chap searching example good hash function for numerical values is the mid-square method the mid-square method squares the key valueand then takes the middle bits of the resultgiving value in the range to this works well because most or all bits of the key value contribute to the result for exampleconsider records whose keys are -digit numbers in base the goal is to hash these key values to table of size ( range of to this range is equivalent to two digits in base that isr if the input is the number squaring yields an -digit number the middle two digits of this result are all digits (equivalentlyall bits when the number is viewed in binarycontribute to the middle two digits of the squared value thusthe result is not dominated by the distribution of the bottom digit or the top digit of the original key value example here is hash function for strings of charactersint (string xint mchar ch[]ch tochararray()int xlength length()int isumfor (sum= = < length() ++sum +ch[ ]return sum mthis function sums the ascii values of the letters in string if the hash table size is smallthis hash function should do good job of distributing strings evenly among the hash table slotsbecause it gives equal weight to all characters this is an example of the folding approach to designing hash function note that the order of the characters in the string has no effect on the result similar method for integers would add the digits of the key valueassuming that there are enough digits to ( keep any one or two digits with bad distribution from skewing the results of the process and ( generate sum much larger than as with many other hash functionsthe final step is to apply the modulus operator to the resultusing table size to generate value within the table range if the sum is not sufficiently largethen the modulus operator will yield poor distribution