Cuckoo Hashing | upper bound on number of elements in our set ; choices for position ; Auxiliary space bounded by a small multiple of MAXN , minimizing wastage ; Array to store possible positions for a key ; function to fill hash table with dummy value * dummy value : let_MIN * number of hashtables : ver ; return hashed value for a key * function : ID of hash function according to which key has to hashed * key : item to be hashed ; function to place a key in one of its possible positions * tableID : table in which key has to be placed , also equal to function according to which key must be hashed * cnt : number of times function has already been called in order to place the first input key * n : maximum number of times function can be recursively called before stopping and declaring presence of cycle ; if function has been recursively called max number of times , stop and declare cycle . Rehash . ; calculate and store possible positions for the key . * check if key already present at any of the positions . If YES , return . ; check if another key is already present at the position for the new key in the table * If YES : place the new key in its position * and place the older key in an alternate position for it in the next table ; else : place the new key in its position ; function to print hash table contents ; function for Cuckoo - hashing keys * keys [ ] : input array of keys * n : size of input array ; initialize hash tables to a dummy value ( let - MIN ) indicating empty position ; start with placing every key at its position in the first hash table according to first hash function ; print the final hash tables ; following array doesn 't have any cycles and  hence all keys will be inserted without any  rehashing  ; following array has a cycle and hence we will have to rehash to position every key
let MAXN = 11 ; let ver = 2 ; let hashtable = new Array ( ver ) ; for ( var i = 0 ; i < hashtable . length ; i ++ ) { hashtable [ i ] = new Array ( 2 ) ; } let pos = Array ( ver ) . fill ( 0 ) ; function initTable ( ) { for ( let j = 0 ; j < MAXN ; j ++ ) for ( let i = 0 ; i < ver ; i ++ ) hashtable [ i ] [ j ] = Number . MIN_VALUE ; } function hash ( functionn , key ) { switch ( functionn ) { case 1 : return key % MAXN ; case 2 : return ( Math . floor ( key / MAXN ) ) % MAXN ; } return Number . MIN_VALUE ; } function place ( key , tableID , cnt , n ) { if ( cnt == n ) { document . write ( key + " " + " " ) ; document . write ( " " + " " ) ; return ; } for ( let i = 0 ; i < ver ; i ++ ) { pos [ i ] = hash ( i + 1 , key ) ; if ( hashtable [ i ] [ pos [ i ] ] == key ) return ; } if ( hashtable [ tableID ] [ pos [ tableID ] ] != Number . MIN_VALUE ) { let dis = hashtable [ tableID ] [ pos [ tableID ] ] ; hashtable [ tableID ] [ pos [ tableID ] ] = key ; place ( dis , ( tableID + 1 ) % ver , cnt + 1 , n ) ; } else hashtable [ tableID ] [ pos [ tableID ] ] = key ; } function printTable ( ) { document . write ( " " + " " ) ; for ( let i = 0 ; i < ver ; i ++ , document . write ( " " ) ) for ( let j = 0 ; j < MAXN ; j ++ ) if ( hashtable [ i ] [ j ] == Number . MIN_VALUE ) document . write ( " " ) ; else document . write ( hashtable [ i ] [ j ] + " " ) ; document . write ( " " ) ; } function cuckoo ( keys , n ) { initTable ( ) ; for ( let i = 0 , cnt = 0 ; i < n ; i ++ , cnt = 0 ) place ( keys [ i ] , 0 , cnt , n ) ; printTable ( ) ; } let keys_1 = [ 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 ] ; let n = keys_1 . length ; cuckoo ( keys_1 , n ) ; let keys_2 = [ 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 , 6 ] ; let m = keys_2 . length ; cuckoo ( keys_2 , m ) ;