Skip to content

Commit e5ea6ef

Browse files
committed
fix: hashing search and delete using wrong table size
1 parent c7f5bd0 commit e5ea6ef

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

src/algorithms/controllers/HashingCommon.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export const PRIMES = [
4545
11, 23, 47, 97
4646
];
4747

48+
// find the table size from the table extracted from the visualizer
49+
export function findTableSize(table) {
50+
for (let i = 0; i < PRIMES.length; i++) {
51+
if (table.length - PRIMES[i] < SPLIT_SIZE) {
52+
return PRIMES[i];
53+
}
54+
}
55+
return 0;
56+
}
57+
4858
// which table size in PRIMES array to allow pointer
4959
export const POINTER_CUT_OFF = 1;
5060

src/algorithms/controllers/HashingDelete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function HashingDelete(
3939

4040
// Assigning parameter values to local variables
4141
const ALGORITHM_NAME = params.name;
42-
const SIZE = params.hashSize; // Hash Modulo being used in the table
42+
const SIZE = table.length; // Hash Modulo being used in the table
4343

4444
// Chunker for intial state of visualizers
4545
chunker.add(

src/algorithms/controllers/HashingInsertion.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export default {
157157
(vis, idx) => {
158158

159159
// Pointer only appear for small table
160-
if (table.length <= PRIMES[POINTER_CUT_OFF]) {
160+
if (table.length < PRIMES[POINTER_CUT_OFF]) {
161161
vis.array.assignVariable(POINTER_VALUE, POINTER, idx);
162162
}
163163

@@ -196,7 +196,7 @@ export default {
196196
(vis, idx) => {
197197

198198
// Pointer only appears for small tables
199-
if (table.length <= PRIMES[POINTER_CUT_OFF]) {
199+
if (table.length < PRIMES[POINTER_CUT_OFF]) {
200200
vis.array.assignVariable(POINTER_VALUE, POINTER, idx);
201201
}
202202
vis.array.fill(INDEX, idx, undefined, undefined, Colors.Pending); // Filling the pending slot with yellow
@@ -425,14 +425,14 @@ export default {
425425
IBookmarks.Init,
426426
(vis, size, array) => {
427427
// Increase Array2D visualizer render space
428-
if (SIZE === LARGE_SIZE) {
429-
vis.array.setSize(3);
430-
vis.array.setZoom(0.7);
431-
vis.graph.setZoom(1.5);
432-
} else {
433-
vis.array.setZoom(1);
434-
vis.graph.setZoom(1);
435-
}
428+
if (SIZE === LARGE_SIZE) {
429+
vis.array.setSize(3);
430+
vis.array.setZoom(0.7);
431+
vis.graph.setZoom(1.5);
432+
} else {
433+
vis.array.setZoom(1);
434+
vis.graph.setZoom(1);
435+
}
436436

437437
// Initialize the array
438438
vis.array.set(array,

src/algorithms/controllers/HashingSearch.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
HASH_TYPE,
1313
PRIMES,
1414
POINTER_CUT_OFF,
15-
newCycle
15+
newCycle,
16+
findTableSize
1617
} from './HashingCommon';
1718

1819
// Bookmarks to link chunker with pseudocode
@@ -55,8 +56,8 @@ export default {
5556
// Assigning parameter values to local variables
5657
const ALGORITHM_NAME = params.name;
5758
const TARGET = params.target; // Target value we are searching for
58-
const SIZE = params.hashSize; // Hash Modulo being used in the table
5959
let table = params.visualisers.array.instance.extractArray(1, EMPTY_CHAR); // The table with inserted values
60+
const SIZE = findTableSize(table); // Hash Modulo being used in the table
6061

6162
// Variable for testing
6263
let found = true;

0 commit comments

Comments
 (0)