Login   Register  
PHP Classes
elePHPant
Icontem

File: flipsort.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of BasicA  >  Flip Sort  >  flipsort.php  >  Download  
File: flipsort.php
Role: ???
Content type: text/plain
Description: php FlipSort Algo with sample test code attached.
Class: Flip Sort
Author: By
Last change:
Date: 2001-03-17 23:16
Size: 1,360 bytes
 

Contents

Class file image Download
	function Flip(&$ArrayIn, $FlipRatio) {

		$j = 0;
	
		for ($i = $FlipRatio; $i >= 0; $i--) {
			$temp[$j] = $ArrayIn[$i]; 
			$j++;
		}
		
		for ($i = $FlipRatio + 1; $i < sizeof($ArrayIn); $i++) {
			$temp[$i] = $ArrayIn[$i];
		}       
		
		$ArrayIn = $temp;
		
	}

	function FlipSort(&$ArrayIn, $Ascending) {
		
		for ($j = sizeof($ArrayIn); $j >= 1; $j--) {

			$pLargest = 0;         
			
			//print $j;

			for ($i = 0; $i < $j; $i++) {
				if ($ArrayIn[$i] > $ArrayIn[$pLargest]) $pLargest = $i;
			}         
			
			//print $ArrayIn[$pLargest] . "<br>";
			
		
			Flip($ArrayIn, $pLargest);
			Flip($ArrayIn, $j - 1);    
			
		}      
		
		if (!($Ascending)) Flip($ArrayIn, sizeof($ArrayIn) - 1); 
	
	}                  
	  
	//if ("Silencer" > "BasicA") print "yes";

	$test[0] = "BasicA";
	$test[1] = "Silencer";
	$test[2] = "GOOOOD";
	$test[3] = "aerospace";
	$test[4] = "Damn";
	 
    FlipSort($test,true);
    
       
    print $test[0] . "<br>";
    print $test[1] . "<br>";
    print $test[2] . "<br>";
    print $test[3] . "<br>";
    print $test[4] . "<br><br><br>";
         

    FlipSort($test,false);
    
       
    print $test[0] . "<br>";
    print $test[1] . "<br>";
    print $test[2] . "<br>";
    print $test[3] . "<br>";
    print $test[4] . "<br>";
?>