<?php
$list = [1,2,3,3,44,5,6,2,3,2,33,8,8,6,"Kanyin",9,24,21,3,'adeola3','Adeola3',44,5,102,33,43,"Kanyin", "Kanyinsola", "Adeola", "adeola", "Adeolas", "Adeola1","Adeola"];
//bubbleSort($list);
binarySearch( 44, $list, 1, 6);
function binarySearch( $target, array $list, $low, $high )
{
if( $low > $high )
{
echo "Not found!";
return false;
}
$middle = ( $low + $high ) / 2;
if( $target == $list[$middle] )
{
echo "Found $target at $middle";
return true;
}
else if($target > $list[ $middle ])
{
return binarySearch( $target, $list, $middle + 1, $high );
}
else{
return binarySearch( $target, $list, $low, $middle - 1 );
}
}
function bubbleSort( array $lst )
{
$numList = count( $lst );
$swapped = false;
do{
$swapped = false;
for( $i=0; $i < $numList - 1; $i++ )
{
if( $lst[$i] > $lst[$i+1]){
$temp = $lst[$i];
$lst[$i] = $lst[$i+1];
$lst[$i+1] = $temp;
$swapped = true;
}
}
} while ( $swapped == true);
echo ( implode(" ", $lst));
}
//selectionSort( $list );
function selectionSort( array $lst )
{
$numList = count( $lst );
for( $i=0; $i < $numList; $i++ )
{
$index = 0;
$smallest = $lst[$i];
for( $j = $i; $j < $numList; $j++ )
{
if( $lst[$j] < $smallest )
{
$smallest = $lst[$j];
$index = $j;
}
$temp = $lst[$i];
$lst[$i] = $smallest;
$lst[$index] = $temp;
}
echo ( implode(" ", $lst));
}
}
//linearSearch( $list, 3 );
function linearSearch( array $lst, $target )
{
$numList = count( $lst );
for( $i=0; $i < $numList; $i++ )
{
if( $lst[$i] == $target )
{
echo "Found $target at $i\n";
}
}
}
//factoral(6);
function factoral( $n )
{
if( $n == 1 )
{
return 1;
}
echo $n * factoral( $n -1 );
}
function chessboard()
{
for($i=0; $i<6; $i++)
{
echo "$i X0 :";
for($j=0; $j<$i; $j++)
{
echo "*";
}
echo "\n";
}
}
//chessboard();
preferences:
49.74 ms | 402 KiB | 5 Q