Location: PHPKode > projects > Data Structures - The flash way! > dsFlash/insertion.php
<?php 
{
session_start();
if(isset($_SESSION['arr']))
{
   if($_POST["text"]=='')
   {
     
     startsort();
     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 startsort()
{
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();
$myText1->setFont($myFont1);
$myText1->setColor(0,255,255);
$myText1->setHeight(40); 

$myText1->addString("The elements in the original 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;
}



$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<20; $i++){ 
$myMovie->nextFrame(); 
$movingSquare[$j]->multColor(1.0-$i/20, 1.0, 1.0); }  //moving the frame after adding the square and number

$k=$k+130;
$j++;
} 



//sort logic
$k=130;
$l=500;
for($i=1;$i<$_SESSION['count'];$i++)
{
$y=$_SESSION['arr'][$i];
     
    $myFont=new SWFFont("bit.fdb"); 
     $myText=new SWFText(); 
     $myText->setFont($myFont);
     $myText->setColor(255,0,255);
     $myText->setHeight(40); 
     $myText->addString("The array element being shifted is : ".$y);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(20,$l);
	 $l=$l+50; 


for($j=$i-1; $j>=0 && $y<$_SESSION['arr'][$j]; $j--)
{

   
     $movingSquare[$j]=$myMovie->add($myShape1[$i]);      //for initial highlighting
     $movingSquare[$j]->moveTo(($j+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'][$j]);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(($k*($j+1))+0.5,100);   //because of array positioning in flash array index is 0 but posn starts with 90*1
	 
	 
	 
	 $movingSquare[$j+1]=$myMovie->add($myShape1[$j+1]); 
     $movingSquare[$j+1]->moveTo(($j+2)*$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'][$j+1]);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(($k*($j+2))+0.5,100); 

   		 for($t1=0; $t1<10; $t1++){ 
             $myMovie->nextFrame(); 
			 $movingSquare[$j]->multColor(1.0, 1.0, 1.0-$t1/10);
             $movingSquare[$j+1]->multColor(1.0, 1.0, 1.0-$t1/10); } 
         for($t2=0; $t2<10; $t2++){ 
             $myMovie->nextFrame(); 
			 $movingSquare[$j]->multColor(1.0-$t2/20, 1.0, 1.0); 
             $movingSquare[$j+1]->multColor(1.0-$t2/20, 1.0, 1.0); }  //changing the color
		

  $_SESSION['arr'][$j+1]=$_SESSION['arr'][$j];
  
  
    
    $movingSquare[$j]=$myMovie->add($myShape1[$i]);      //for initial highlighting
     $movingSquare[$j]->moveTo(($j+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'][$j]);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(($k*($j+1))+0.5,100);   //because of array positioning in flash array index is 0 but posn starts with 90*1
	 
	 
	 
	 $movingSquare[$j+1]=$myMovie->add($myShape1[$j+1]); 
     $movingSquare[$j+1]->moveTo(($j+2)*$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'][$j+1]);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(($k*($j+2))+0.5,100); 

   		 for($t1=0; $t1<10; $t1++){ 
             $myMovie->nextFrame(); 
			 $movingSquare[$j]->multColor(1.0, 1.0, 1.0-$t1/10);
             $movingSquare[$j+1]->multColor(1.0, 1.0, 1.0-$t1/10); } 
         for($t2=0; $t2<10; $t2++){ 
             $myMovie->nextFrame(); 
			 $movingSquare[$j]->multColor(1.0-$t2/20, 1.0, 1.0); 
             $movingSquare[$j+1]->multColor(1.0-$t2/20, 1.0, 1.0); }  //changing the color
		
  
}


$_SESSION['arr'][$j+1]=$y;

     
	 $movingSquare[$j+1]=$myMovie->add($myShape1[$j+1]); 
     $movingSquare[$j+1]->moveTo(($j+2)*$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'][$j+1]);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(($k*($j+2))+0.5,100); 


     $movingSquare[$i]=$myMovie->add($myShape1[$i]);      //for initial highlighting
     $movingSquare[$i]->moveTo(($i+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'][$i]);
     $firstText=$myMovie->add($myText); 
     $firstText->moveTo(($k*($i+1))+0.5,100);   //because of array positioning in flash array index is 0 but posn starts with 90*1
	 
	 
	 
   		 for($t1=0; $t1<10; $t1++){ 
             $myMovie->nextFrame(); 
			 $movingSquare[$i]->multColor(1.0, 1.0, 1.0-$t1/10);
             $movingSquare[$j+1]->multColor(1.0, 1.0, 1.0-$t1/10); } 
         for($t2=0; $t2<10; $t2++){ 
             $myMovie->nextFrame(); 
			 $movingSquare[$i]->multColor(1.0-$t2/20, 1.0, 1.0); 
             $movingSquare[$j+1]->multColor(1.0-$t2/20, 1.0, 1.0); }  //changing the color
		
    





    
}


$myMovie->save("insertion.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">
<!--
.style2 {font-size: 12}
.style3 {font-size: 12pt}
.style5 {
	font-size: 12pt;
	color: #663333;
	font-weight: bold;
}
.style6 {color: #3300CC}
.style8 {font-size: 12; 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" >
		<EMBED  src="insertion.swf" width=554 HEIGHT=500 autostart="true" loop="false" TYPE="application/x-shockwave-flash" PLUGINSPAGE= "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" bgcolor="#663333">		<br>

            <form name="f" action = "<? echo $_SERVER['PHP_SELF']?>" method="post">
			Enter one more number(Press Sort without entering any value to start the animation): <input type="text" name="text" value="<?=$text?>"  size="30"><br>
			<input type="submit" name="Sort"  ><br>
			*-Press submit after entering every value of the array
            </form>
			<br>
			   <form action="down.php" name="fff" method="post">
			   <input type="hidden" name="file"  value="insertion.swf">
			   <input type="submit"  name = "click to download the flash" value="Download the generated flash!">
			   </form>
			
	</div>
</div>

<p class="style2"><span class="style5">ALGORITHM FOR INSERTION SORT </span></p>
<p class="style8"><span class="style3"><br>
    <span class="style6">/*Initially x[0] may be thought of as a sorted file of one element */<br>
  /*After each repetition of the following loop, the elements x[0] */<br>
  /*through x[k] are in order.*/<br>
  for(k=1;k less_than n; k++)<br>
  {<br>
  /*insert x[k] into the sorted file*/<br>
  y=x[k];<br>
  /*move down 1 position all elements greater than y*/<br>
  for(i=k-1;i>=0 && y less_than x[i];i--)<br>
  {<br>
  x[i+1]=x[i];<br>
  }<br>
  /*insert y at proper position*/<br>
  x[i+1]=y;<br>
  }<br>
  </span></span></p>
<p class="style8">&nbsp;</p>
<p class="style8"><span class="style6"><br>
</span></p>
</body>
</html>
Return current item: Data Structures - The flash way!