```
<html>
<head>
<?php
$page = $_SERVER['php_SELF'];
?>
</head>
<?php
/* johnpfeiffer 18jan09 program to allow sorting of files/dirs by date/size
since I cannot use a struct I intend on having 3 arrays and use their index (key)
integers as "pointer references" for when displaying by filesize or date
The question is, as always, to load up the processor or use up the RAM, in this case I
choose more arrays as any ordering by size or date will still need to store...
hmmmm... an alternative is only 1 more array that will have the sorted output of the first
variables:
$dh //directory handle
$i // counter
$file //handle to a file object
$dirlist //array of file objects that happen to be directories
$filelist //array of file objects
$filesize //array of sizes of the above file objects
etc.
*/
$dh = opendir( getcwd() ); //get current working directory
$i=0; //counter for multi-dim array of files
$j=0;
while (false !== ($file = readdir($dh)))
{
if( $file != "." && $file != ".." ) //don't test the root directories
{
if( is_file($file) )
{
$filelist[$i][0] = $file;
$filelist[$i][1] = filesize($file);
$filelist[$i][2] = filemtime($file);
$i++;
$sorted_array[$i] = $i; //this array will be our index of
}
else
{ $dirlist[] = $file; }
}
}
echo $i . "<br \> <br \>\n";
print_r($filelist);
/* an array can imitate a btree if you think 0 = index of data, 1 = arr loc of left (3)
2 = arr loc of right, 3 = index of data and so on n + 1 and n + 2 for left & right
this "sorted" btree array will only contain as data the index of another array
that contains the "real data", along with the index of a left branch (or -1)
and the index of a right branch (or -1)
0 1 2 - 3 4 5 6 7 8 - 9 10 11 12 13 14 15 16 17 18 19 20 -
d l r
ALSO, keep in mind that the way arrays can be used in php, they might as well
be a linked list...
The difficulty is that there are no "reverse" pointers from child to parent, so it's
important to "traverse" the btree (array) with a stack which will preserve the
order in which items are pushed and popped
??
a memory large (but perhaps very fast) version would have each node in sets of
three -> the parent and two children, this would guarantee that we could use
an exp formula to quickly traverse all left branches (e.g. 1 -> 3 -> 9 -> 15
?
*/
$original_loc = 0;
$sorted_loc = 0;
$filelist_size = count($filelist);
while ( $original_loc < $filelist_size)
{
//create new node
$sorted_filelist[ $sorted_loc ] = $original_loc; //loc of the data of the current node
$sorted_filelist[ $sorted_loc + 1 ] = -1; //left branch set to invalid location
$sorted_filelist[ $sorted_loc + 2 ] = -1; //right branch set to invalid location
if( $original_loc + 1 < $filelist_size ) //this will be true until the last node
{ //peek ahead to fill the branch data...
if( $filelist[ $original_loc + 1 ] < $filelist[ $original_loc ] )
{ //then left branch is at the next open loc
$sorted_filelist[ $sorted_loc + 1 ] = $sorted_loc + 3; //always 3 more for space
}
else if( $filelist[ $original_loc ] < $filelist[ $original_loc + 1 ] )
{ //then right branch is at the next open loc
$sorted_filelist[ $sorted_loc + 2 ] = $sorted_loc + 3;
}
else //items are equal, for now, not possible...
{
echo "serious error, duplicate items in original array";
}
$sorted_loc = $sorted_loc + 3;
}/* end if original array has 2 or more elements left */
$i++;
}/* end while not larger than original array size */
echo "<br \> <br \>\n";
echo count($filelist);
for ($k=1;$k<=10;$k++)
{
$example[$k] = rand(100, 200);
}
print_r($example);
/* need to implement either binary tree or mergesort? - example walkthru
f[] = 2 7 4
i=0, k=0, s[]= ...
while( 0 < 3) TRUE
s[0] = 0 (2)
s[1] = -1
s[2] = -1
if( 0 + 1 < 3 )
{
if( f[0 + 1]=7 < f[0]=2 )
FALSE
else if( f[0]=2 < f[0 + 1]=7 )
TRUE
then s[2] = 0+3 //the next open spot
}
i=1, k=3, s[]= 0,-1,3...
- - - - - - - - - - - - - - - - - -
while( 1 < 3) TRUE
s[3] = 1 (7)
s[4] = -1
s[5] = -1
if( 1 + 1 < 3 )
{
if( f[1 + 1]=4 < f[1]=7 )
TRUE
then s[4] = 6 //the next open spot
}
i=2, k=6, s[]= 0,-1,3, 1,6,-1
- - - - - - - - - - - - - - - - - -
while( 2 < 3) TRUE
s[6] = 2 (4)
s[7] = -1
s[8] = -1
if( 2 + 1 < 3) FALSE
i=3
while( 3< 3) FALSE
END OF WALKTHRU - successful algorithm matches initial data
*/
?>
</body>
</html>
```