Index.php
<html>
<head>
<title>Assignment - 17</title>
</head>
<frameset cols="20%,80%">
<frame name="left" src="left.php"/>
<frame name="right" src=""/>
</frameset>
</html>
Left.php
<html>
<head>
<title>Demo</title>
<base href="" target="right">
</head>
<body>
<a href="bubble.php" >Bubble Sort</a><br/>
<a href="linear.php" >Linear Search</a>
</body>
</html>
Bubble.php
<html>
<head>
<title>Bubble sort</title>
</head>
<body>
<form action="" method="post">
Enter numbers : <input type="text" name="num"/><br/>
<input type="submit"/>
<br/>
<?php
if($_POST)
{
//get the post value from form
$numbers = $_POST['num'];
//separate the numbers and make into array
$arr = explode(',', $numbers);
function bubbleSort(array $arr)
{
$n = sizeof($arr);
for ($i = 1; $i < $n; $i++) {
for ($j = $n - 1; $j >= $i; $j--) {
if($arr[$j-1] > $arr[$j]) {
$tmp = $arr[$j - 1];
$arr[$j - 1] = $arr[$j];
$arr[$j] = $tmp;
}
}
}
return $arr;
}
$result = bubbleSort($arr);
print_r($result);
}
?>
</form>
</body>
</html>
Linear.php
<html>
<head>
<title>Linear Search</title>
</head>
<body>
<form method="post">
Enter numbers : <input type="text" name="num"/><br/>
Enter number to search : <input type="text" name="value"/><br/>
<input type="submit"/>
<br/>
<?php
if($_POST)
{
//get the post value from form
$numbers = $_POST['num'];
//separate the numbers and make into array
$arr = explode(',', $numbers);
$value = $_POST['value'];
function linear_search(&$arr, $value) {
for ($i = 0; $i < sizeof($arr); $i++) {
if ($arr[$i] == $value) {
return $i;
}
}
return -1;
}
# Print numbers to search
# Find the index of 'value'
$index = linear_search($arr, $value) + 1;
# Print the index where 'value' is located
echo "\nNumber $value is at index $index\n";
}
?>
</form>
</body>
</html>
No comments:
Post a Comment