From 2d5ffe74fe63fe7b29640986207961ef58cdee63 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Wed, 23 Oct 2019 15:55:38 +0000 Subject: [PATCH] Completed the algorithm. --- hashtables.html | 32 ++++++++++++++++++++------------ 1 files changed, 20 insertions(+), 12 deletions(-) diff --git a/hashtables.html b/hashtables.html index 755d9fa..084cf3f 100644 --- a/hashtables.html +++ b/hashtables.html @@ -45,6 +45,7 @@ var names; var phones; var memory; +var backupmemory; var insert_idx=0; var insert_idx_without_collision=0; function hash(stringval){ @@ -85,24 +86,28 @@ function insertMissingIntoOpen(data_index){ insert_idx=insert_index_without_collision; - if(data_index==-1) data_index=insert_index_without_collision; + if(data_index==-1) data_index=insert_idx; hashval=hash(names[data_index]); console.log("f"+hashval); - while(memory[hashval]!=-1) hashval=(hashval+1)%13; - memory[hashval]=data_index; + while(backupmemory[hashval]!=-1) hashval=(hashval+1)%13; + backupmemory[hashval]=data_index; insert_idx++; - if(insert_idx==8){ - alert("Completed!"); - } - else { - currentInner(document.getElementById("open1current"), insert_idx); + insert_index_without_collision++; + //else { + console.log(names[data_index] + "==> REMAPPED ==>" + hashval); el1=document.getElementById("o"+hashval); el2=document.getElementById("of"+hashval); el1.innerHTML=names[data_index]; el2.innerHTML=phones[data_index]; -} + if(insert_idx==8){ + alert("Completed!"); + document.getElementById("open1current").innerHTML="DONE!"; + } else{ + currentInner(document.getElementById("open1current"), insert_idx); + } +//} } @@ -131,6 +136,7 @@ el2.innerHTML=phones[data_index]; memory[hashval]=data_index; + backupmemory[hashval]=data_index; insert_idx++; if(insert_idx==8){ alert("Completed!"); @@ -157,9 +163,10 @@ ]; phones=[1,2,3,4,5,6,7,8] for(i=0;i<8;i++){ - phones[i]="(01)"+Math.floor((Math.random() * 1000) + 1)+"-"+Math.floor((Math.random() * 1000) + 1); + phones[i]="(01)"+Math.floor((Math.random() * 9000) + 1000)+"-"+Math.floor((Math.random() * 900) + 100); } memory=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]; + backupmemory=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]; /* Slide 1 actions */ @@ -176,7 +183,8 @@ document.getElementById("intro0").innerHTML=names[0]; document.getElementById("intro1").innerHTML=names[0]; document.getElementById("s0result").innerHTML=document.getElementById("s0").innerHTML; -document.getElementById("s0result").innerHTML=document.getElementById("n0").innerHTML; +document.getElementById("s3value").innerHTML=document.getElementById("s3").innerHTML; +document.getElementById("s0number").innerHTML=document.getElementById("n0").innerHTML; /* End Slide 1 */ /*Filling the values into array demo until collision occurs */ @@ -265,7 +273,7 @@ <span style="width:150px;height:110px;border:1px solid #000;margin:0px;display:inline-block;margin-left:-8px;"><div style="background:grey;color:white">data[1]</div><div id="s1">Obi</div><div id="n1" style="font-size:15pt">(01)222-2222</div></span> <span style="width:150px;height:110px;border:1px solid #000;margin:0px;display:inline-block;margin-left:-8px;"><div style="background:grey;color:white">data[2]</div><div id="s2">Luke</div><div id="n2" style="font-size:15pt">(01)123-1234</div></span> <span style="width:150px;height:110px;border:1px solid #000;margin:0px;display:inline-block;margin-left:-8px;"><div style="background:grey;color:white">data[3]</div><div id="s3">Darth</div><div id="n3" style="font-size:15pt">(01)111-1111</div></span> - <p style="margin-top:30px;font-size:15pt">e.g.: Accessing the item at memory location data[0] yields name of the person <b><span id="s0result">Yoda</span></b> and person's phone number: <span id="s0number"></span>(01)123-1234</p><br /> + <p style="margin-top:30px;font-size:15pt">e.g.: Accessing the item at memory location data[0] yields name of the person <b><span id="s0result">Yoda</span></b> and person's phone number: <span id="s0number">(01)123-1234</span></p><br /> <p>Finding phone number of <span id="s3value">Darth</span> requires linear search through the array, which is not efficient.</p> -- Gitblit v1.9.3