Решение коллизий методом открытой адресации(не важно C# или C++)
Формулировка задачи:
Посмотрите пожалуйста на код. Не могу понять в чем проблема. Алгоритм с книги Т.Кормена ( ссилка - > https://brb.to/texts/other/i48NWg7CO...-i-analiz.html). Потом проверял етот алгоритм -> Хэш-таблица. Он работает правильно. Переписал под свою лабу - коллизии не решаються.
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hash2
{
class Program
{
static Int32 m = 11;
static Int32[] hash = new Int32[m];// hash table
static Int32 c1 = 1, c2 = 3;
static Int32 m_ = m - 1;
static void Main(string[] args)
{
// int []arr = new int[m];
for (int i = 0; i < hash.Length; i++)
{
hash[i] = -1;
}//-1 = means that item in hash is empty
HashInsert(10);
HashInsert(22);
HashInsert(31);
HashInsert(4);
HashInsert(15);
HashInsert(28);
HashInsert(17);
HashInsert(88);
HashInsert(59);
Console.Read();
}
static Int32 h(Int32 k)//different hash functions
{
return k % m;
}
static Int32 h_linear(Int32 k, Int32 i)
{
return (h(k) + 1) % m;
}
static Int32 h_quad(Int32 k, Int32 i)
{
return ((h(k) + c1 * i + c2 * i * i) % m);
}
static Int32 h2(Int32 k) { return 1 + (k % (m - 1)); }
static Int32 h_double(Int32 k, Int32 i)
{
return (h(k) + i * h2(k)) % m;
}
static Int32 HashSearch(Int32 k)
{
Int32 i = 0;
int j = 0;
do
{
j = h_linear(k, i);
if (hash[j] == k) return j;
else i++;
} while (hash[j] != -1 && i != m);
return -1;
}
static Int32 HashInsert(Int32 k)
{
Int32 i = 0;
do
{
int j = h_linear(k, i);
{
if (hash[j] == -1)
{
hash[j] = k;
Console.WriteLine("index = :" + j+" Value: "+k);
return j;
}
else i++;
}
} while (i != m);
Console.WriteLine("index = : "+-1);
return -1;
}
}
}Решение задачи: «Решение коллизий методом открытой адресации(не важно C# или C++)»
textual
Листинг программы
static Int32 h_linear(Int32 k, Int32 i)
{
return (h(k) + 1) % m;
}