-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbubblesort.php
99 lines (90 loc) · 2.36 KB
/
bubblesort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html>
<head>
<title>Search and Sort</title>
</head>
<body>
<?php
function bubbleSort($arr){
for($i = 0; $i < (count($arr) - 1);$i++){
$arr[$i] = (int) $arr[$i];
//i represents how many elements have bubbled to correct place
for($j = 0; ($j + 1) < count($arr); $j++){
$arr[$j] = (int) $arr[$j];
if($arr[$j] < $arr[$j + 1]){
//the swap
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return implode(', ', $arr);
}
if(isset($_POST['sort'])){
$input = $_POST['numbers'];
$err = '';
if($input == ""){
$err = "Please enter valid inputs";
} elseif(!is_string($input)){
$err = "Please enter valid string in this order e.g 2,3,4,";
} else {
$arr = explode(",", $input);
$ret = bubbleSort($arr);
}
}
?>
<div class="container">
<form action="" method="post">
<input type="text" style="width: 250px;height: 35px" name="numbers" id="numbers" value="<?php echo (isset($input))? $input : ""?>" placeholder="e.g 21,4,3,5,66,63,22"><br>
<span id="mssg"></span><br>
<button onclick="return sortr();">Javascript Sort</button>
<button type="submit" name="sort">PHP Sort</button><br>
<output style="width: 250px;height: 35px" name="answers" id="answers" ><?php echo (isset($ret))? $ret : ""?></output>
</form>
</div>
<br>
<div>
<ul>
<li>
<a href="index.php">Home</a>
</li>
<li>
<a href="searchandsort.php">Selection sort</a>
</li>
</ul>
</div>
</body>
<script type="text/javascript">
function sortr(){
input = document.getElementById('numbers').value;
err = document.getElementById('mssg');
if(input == ""){
err.innerText = "Please enter valid inputs";
} else if(typeof input !== "string"){
err.innerText = "Please enter valid string in this order e.g 2,3,4,";
} else {
arr = input.split(",");
ret = bubbleSort(arr);
document.getElementById('answers').innerText = ret;
}
return false;
}
function bubbleSort(arr){
for(i = 0; i < arr.length - 1;i++){
arr[i] = parseInt(arr[i]);
//i represents how many elements have bubbled to correct place
for(j = 0; j + 1 < arr.length; j++){
arr[j] = parseInt(arr[j]);
if(arr[j] < arr[j + 1]){
//the swap
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
</script>
</html>