-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-test.php
116 lines (90 loc) · 2.24 KB
/
sort-test.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Sorting Algorithm Examples in PHP
*
* Matt Fisher - http://www.fisherinnovation.com
* Github - https://github.com/fisherinnovation/Sorting-Algorithms
*/
$n = 1000; // Number of elements to sort.
$sortlist = array(); // List of intergers.
for($i = 0; $i < $n; $i++) array_push($sortlist, $i);
/**
* Randomizes the lists of integers.
*/
function randomizeList() {
global $sortlist;
shuffle($sortlist);
}
/**
* Bubble Sort
*/
function bubbleSort($arr) {
$time_begin = microtime(true);
$size = count($arr);
for ($i=0; $i<$size; $i++) {
for ($j=0; $j<$size-1-$i; $j++) {
if ($arr[$j+1] < $arr[$j]) {
swap($arr, $j, $j+1);
}
}
}
$time_end = microtime(true) - $time_begin;
echo 'Bubble Sort: ' . $time_end . ' seconds.<br>';
}
/**
* Swaps two elements in a array.
* Used by Bubble Sort.
*/
function swap(&$arr, $a, $b) {
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
}
/**
* Sort an array and maintain index association.
* Uses PHP asort.
*/
function associativeSort($list) {
$time_begin = microtime(true);
asort($list);
$time_end = microtime(true) - $time_begin;
echo 'Associative Sort: ' . $time_end . ' seconds.<br>';
}
/**
* Sort an array by key
* Uses PHP ksort.
*/
function keySort($list) {
$time_begin = microtime(true);
ksort($list);
$time_end = microtime(true) - $time_begin;
echo 'Key Sort: ' . $time_end . ' seconds.<br>';
}
/**
* Insertion Sort.
*/
function insertionSort($list) {
$time_begin = microtime(true);
$l = sizeof($list);
for($i = 0; $i < $l; $i++) {
$save = $list[$i];
$j = $i;
while($j > 0 && $list[$j - 1] > $save) {
$list[$j] = $list[$j - 1];
$j -= 1;
}
$list[$j] = $save;
}
$time_end = microtime(true) - $time_begin;
echo 'Insertion Sort: ' . $time_end . ' seconds.<br>';
}
print "Benchmarking Sorting Algorithms <br>" . $n . " randomized integers<hr/>";
randomizeList();
bubbleSort($sortlist);
randomizeList();
associativeSort($sortlist);
randomizeList();
keySort($sortlist);
randomizeList();
insertionSort($sortlist);
?>