<?php
{
session_start();
if(isset($_SESSION['arr']))
{
if($_POST["text"]=='')
{
startsearch($_POST["search"]);
unset($_SESSION['arr']);
$_SESSION['count']=0;
$_SESSION['arr']=array();
}
else
{
array_push($_SESSION['arr'],$_POST["text"]);
$_SESSION['count']=$_SESSION['count']+1;
}
}
else
{
$_SESSION['count']=0;
$_SESSION['arr']=array();
}
}
?>
<?php
function startsearch($srch)
{
for($i=0;$i<$_SESSION['count'];$i++)
$myShape1[$i]=new SWFShape();
for($i=0;$i<$_SESSION['count'];$i++)
{
$myShape1[$i]->setLine(5,0,0,255); //WIDTH of the line and its rgb value
$myShape1[$i]->setRightFill(255,0,0);
$myShape1[$i]->movePen(-15,-65);
$myShape1[$i]->drawLine(130,0);
$myShape1[$i]->drawLine(0,130);
$myShape1[$i]->drawLine(-130,0);
$myShape1[$i]->drawLine(0,-130);
}
$myMovie=new SWFMovie();
$myMovie->setDimension(1200,1200);
$myMovie->setBackground(255,0,0);//red
//print the search term and array elements
$myFont1=new SWFFont("bit.fdb");
$myText1=new SWFText();
$myText3=new SWFText();
$myText4=new SWFText();
$myText4->setFont($myFont1);
$myText4->setColor(0,255,255);
$myText4->setHeight(40);
$myText1->setFont($myFont1);
$myText1->setColor(0,255,255);
$myText1->setHeight(40);
$myText3->setFont($myFont1);
$myText3->setColor(0,255,255);
$myText3->setHeight(40);
$myText1->addString("The elements in the array are");
$firstText1=$myMovie->add($myText1);
$firstText1->moveTo(20,350);
$k=20;
foreach ($_SESSION['arr'] as $val)
{
$myText2=new SWFText();
$myText2->setFont($myFont1);
$myText2->setColor(0,255,255);
$myText2->setHeight(40);
$myText2->addString($val);
$firstText2=$myMovie->add($myText2);
$firstText2->moveTo($k,400);
$k=$k+120;
}
$myText3->addString("The search value is ");
$firstText3=$myMovie->add($myText3);
$firstText3->moveTo(20,500);
$myText4->addString($srch);
$firstText4=$myMovie->add($myText4);
$firstText4->moveTo(500,500);
$k=130;
$j=0;
foreach ($_SESSION['arr'] as $val) //adding each shape as a moving square with text
{
$movingSquare[$j]=$myMovie->add($myShape1[$j]);
$movingSquare[$j]->moveTo($k,100);
$myFont=new SWFFont("bit.fdb");
$myText=new SWFText();
$myText->setFont($myFont);
$myText->setColor(255,0,255);
$myText->setHeight(40);
$myText->addString($val);
$firstText=$myMovie->add($myText);
$firstText->moveTo($k+0.5,100);
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
$movingSquare[$j]->multColor(1.0-$i/10, 1.0, 1.0); } //moving the frame after adding the square and number
$k=$k+130;
$j++;
}
// search logic
$p=1; // num of boxes to move front or back
$k=130;
$j=0;
$left=0;
$flag=0;
$right=$_SESSION['count']-1;
while($left<=$right)
{
$mid=(int)(($left+$right)/2);
$movingSquare[$mid]=$myMovie->add($myShape1[$mid]);
$movingSquare[$mid]->moveTo(($mid+1)*$k,100);
$myFont=new SWFFont("bit.fdb");
$myText=new SWFText();
$myText->setFont($myFont);
$myText->setColor(255,0,255);
$myText->setHeight(40);
$myText->addString($_SESSION['arr'][$mid]);
$firstText=$myMovie->add($myText);
$firstText->moveTo($k*($mid+1)+0.5,100);
if($srch==$_SESSION['arr'][$mid])
{
$flag=1;
for($i=0; $i<10; $i++){
$myMovie->nextFrame(); // if found colour is changed
$movingSquare[$mid]->multColor(1.0, 1.0, 1.0-$i/10); }
$myFont6=new SWFFont("bit.fdb");
$myText6=new SWFText();
$myText6->setFont($myFont6);
$myText6->setColor(0,0,255);
$myText6->setHeight(40);
$myText6->addString("Element found at position ".($mid+1));
$firstText6=$myMovie->add($myText6);
$firstText6->moveTo(20,600);
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
}
break;
}
if($srch<$_SESSION['arr'][$mid])
{
$right=$mid-1; //if not found color changed and set back to orig color
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
$movingSquare[$mid]->multColor(1.0, 1.0, 1.0-$i/10); }
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
$movingSquare[$mid]->multColor(1.0-$i/10, 1.0, 1.0); }
$myFont7=new SWFFont("bit.fdb");
$myText7=new SWFText();
$myText7->setFont($myFont7);
$myText7->setColor(0,0,255);
$myText7->setHeight(40);
$myText7->addString("The sub array that is being searched is between ".($left+1)." and ".($right+1));
$firstText7=$myMovie->add($myText7);
$firstText7->moveTo(20,700);
for($i=0; $i<10; $i++){
$myMovie->nextFrame(); }
}
else
{
$left=$mid+1;
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
$movingSquare[$mid]->multColor(1.0, 1.0, 1.0-$i/10); }
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
$movingSquare[$mid]->multColor(1.0-$i/10, 1.0, 1.0); }
$myFont7=new SWFFont("bit.fdb");
$myText7=new SWFText();
$myText7->setFont($myFont7);
$myText7->setColor(0,0,255);
$myText7->setHeight(40);
$myText7->addString("The sub array that is being searched is between ".($left+1)." and ".($right+1));
$firstText7=$myMovie->add($myText7);
$firstText7->moveTo(20,700);
for($i=0; $i<10; $i++){
$myMovie->nextFrame(); }
}
}//while end
if($flag==0)
{
$myFont6=new SWFFont("bit.fdb");
$myText6=new SWFText();
$myText6->setFont($myFont6);
$myText6->setColor(0,0,255);
$myText6->setHeight(40);
$myText6->addString("Element not in array");
$firstText6=$myMovie->add($myText6);
$firstText6->moveTo(20,600);
for($i=0; $i<10; $i++){
$myMovie->nextFrame();
}
}
$myMovie->save("binary.swf");
}
?>
<html>
<head>
<script type='text/javascript' src='recoverscroll.js'> </script>
<title>Learn Data Structures the Flash Way</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
<style type="text/css">
<!--
.style3 {font-size: 12pt}
.style4 {
font-size: 12pt;
font-weight: bold;
color: #663333;
}
.style5 {
color: #3300FF;
font-weight: bold;
}
-->
</style>
</head>
<body onLoad="document.f.text.focus()">
<script type='text/javascript'>
RecoverScroll.addToHandler(window,'onload',function(){RecoverScroll.init()});
</script>
<div id="container">
<div id="header">
<h2>Let's learn, here's my card.</h2>
<h1><a href="#">Data Structures Tutor </a></h1>
</div>
<div id="navigation">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href=#>Animated Algorithms</a>
<ul>
<li><a href="linear.php">Linear search</a></li><br />
<li><a href="binary.php">Binary search</a></li><br />
<li><a href="bubble.php">Bubble sort</a></li><br />
<li><a href="selection.php">Selection sort</a></li><br />
<li><a href="insertion.php">Insertion sort</a></li><br />
<li><a href="shell.php">Shell sort</a></li><br />
</ul>
</li>
</ul>
</div>
<div id="content">
<object id="here">
<EMBED src="binary.swf" WIDTH=554 HEIGHT=500 loop="false" TYPE="application/x-shockwave-flash" PLUGINSPAGE= "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" bgcolor="#663333"></EMBED></object>
<br>
<form name="f" action = "<? echo $_SERVER['PHP_SELF']?>" method="post">
Array value (Enter the values in ascending order):
<input type="text" name="text" value="<?=$text?>" size="30"><br><br>
Search value: <input type="text" name="search" value="<?=$search?>" size="30"><br><br>
<input type="submit" ><br>
*-Press submit after entering every value of the array and finally enter the search value
</form>
<br>
<form action="down.php" name="fff" method="post">
<input type="hidden" name="file" value="binary.swf">
<input type="submit" name = "click to download the flash" value="Download the generated flash!">
</form>
</div>
</div>
<br>
<div id="div"></div>
<p class="style4">ALGORITHM FOR BINARY SEARCH </p>
<p class="style5"><br>
<span class="style3">1.Start
<br>
2.Get input elements as 'array','count' as the number of elements
<br>
3.Get the search element 'search'
<br>
4.left=0,right=count
<br>
mid=(left+right)/2
<br>
5.while(left less_than or equals right) do
<br>
5.1 if(array[mid] equals search)
<br>
{
<br>
print "element found at position"[index] <br>
break
<br>
}
<br>
5.2 else if(array[mid] greater_than search)
<br>
{
<br>
right=mid-1
<br>
mid=(left+right)/2
<br>
}
<br>
5.3 else if(array[mid] less_than search)
<br>
{
<br>
left=mid+1
<br>
mid=(left+right)/2
<br>
}
<br>
6.Stop</span></p>
<p class="style5"> </p>
</body>
</html>