public class HashQuadratisch { public static int empty = -Integer.MAX_VALUE; public int[] table = {empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty}; public int hash(int x) { return (3*x+5)%table.length; } public void insert(int x) { if (table[hash(x)] == empty) { table[hash(x)] = x; } else { for (int i = 1; i < table.length; i++) { if (table[(hash(x)+(int)Math.pow(i,2))%table.length] == empty) { table[(hash(x)+(int)Math.pow(i,2))%table.length] = x; break; } } } } public boolean contains(int x) { if (table[hash(x)] == x) { return true; } else { for (int i = 1; i < table.length; i++) { if (table[(hash(x)+(int)Math.pow(i,2))%table.length] == x) { return true; } } } return false; } public static void main(String[] args) { HashQuadratisch HQ = new HashQuadratisch(); HQ.insert(5); HQ.insert(1); HQ.insert(19); HQ.insert(23); HQ.insert(14); HQ.insert(17); HQ.insert(32); HQ.insert(30); HQ.insert(2); HQ.insert(7); HQ.insert(10); for (int i = 0; i < HQ.table.length; i++) System.out.println(i + ":" + ((HQ.table[i] != empty) ? HQ.table[i] : "")); } }