-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.php
More file actions
80 lines (73 loc) · 2.07 KB
/
selection_sort.php
File metadata and controls
80 lines (73 loc) · 2.07 KB
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
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8" />
<meta name="description" content="" />
<title>Selection Sort</title>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/dark-hive/jquery-ui.css"/>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function(){
});
</script>
</head>
<!-- PHP DECLARE FUNCTIONS AND VARIABLES -->
<?php
// Generate random array
function random_array($size, $min, $max)
{
for ($index = 0; $index < $size; $index++)
{
$rand_value = rand($min, $max);
$array[] = $rand_value;
}
return $array;
}
function selection_sort($array)
{
// Declare Variables
$array_length = count($array);
$min_val = $array[0];
// Sort the array
for ($index = 0; $index < $array_length; $index++)
{
// Stagger indexes to compare minimum values
$sort_index = $index;
// Get current minimum value in array
for ($sort_index; $sort_index < $array_length; $sort_index++) {
if ($min_val > $array[$sort_index] || $min_val == null)
{
$min_val = $array[$sort_index];
$temp = $sort_index;
}
}
// Store current array postion in a temporary variable
$temp_position = $array[$index];
// Set next sorted position in array to minimum value
$array[$index] = $min_val;
// Store temporary variable wher minimum value was found
$array[$temp] = $temp_position;
// Set minimum value to null
$min_val = null;
}
return $array;
}
// Declare Variables
$array_size = 100;
$min = 0;
$max = 10000;
?>
<!-- END OF PHP DECLARATIONS -->
<body>
<div class="container">
<?php
$random_array = random_array($array_size, $min, $max);
var_dump($random_array);
$sorted_array = selection_sort($random_array);
var_dump($sorted_array);
?>
</div> <!--End of #container -->
</body>
</html>