python - Compare linear probing with quadratic probing to see which one results in fewer collisions, given different load factors of the hash table -


i need implement insert function takes key, hash table, , probe function arguments , inserts key hash table, using probe function whenever collision occurs. insert function returns number of collisions occurred during insertion. below code coded not sure whether going in right direction or not.

import random import math  def linear_probe(random_list, m):     hash_table = [none]*m     count = 0     in random_list:         stop = false         hash = % m         while not stop:             if hash_table[hash] == none:                 hash_table[hash] =                 stop = true             else:                 hash = (hash+1) % m                 count +=1     return count  def quadratic_probe(random_list, m):     hash_table = [none]*m     count = 0     in random_list:         j = 1         stop = false         hash = % m         while not stop:             if hash_table[hash] == none:                 hash_table[hash] =                 stop = true             else:                 hash = (hash+j) % m                 count +=1                 j = int((math.sqrt(j)+1) ** 2)     return count     def comp_lists(list1, list2): def comp_elem(elem1, elem2): return 1 if elem1 < elem2 else 2 return map(comp_elem, list1, list2)  m = 1009 random_list=random.sample(range(10000), 1000)  count1 = linear_probe(random_list, m)  count2 = quadratic_probe(random_list, m)  print('linear probe collisions:',count1) print('quadratic probe collisions:',count2) 


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -