-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.php
More file actions
76 lines (66 loc) · 1.9 KB
/
bubbleSort.php
File metadata and controls
76 lines (66 loc) · 1.9 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
<?php
// Author: Marguerite Desko
// This will do a bubble sort of an array filled
// with random numbers
// can call with desired # of array elements and
// low digit for variability/ calculating the max value
// of the random numbers
// defaults to 7000, 3
//$debug = TRUE; set to true if debugging this
// require class that makes arrays
require_once("makeNumericArray.php");
// this function will do a slow "bubble sort" of an array
function bubbleSort ($array) {
$arrayCount = count($array);
$swapped = TRUE; // set value to true so while initializes
// until nothing else in the row needs to be swapped, keep
// attempting to sort values
while ($swapped) {
// this particular row is not yet swapped
$swapped = FALSE;
// set up iterator for counting backwards from sorted section
// that results at end of array
$j++;
// go through the array, element by element
for ($i = 0; $i < $arrayCount - $j; $i ++) {
// switch the positions of the elements if a lower value
// is after a higher value
if ($array[$i] > $array[$i + 1]) {
$holdValue = $array[$i];
$array[$i] = $array[$i + 1];
$array[$i + 1] = $holdValue;
$swapped = TRUE;
}
}
}
// return the sorted array from the function
return ($array);
}
// if command line values, set them
if ($argv[1]) {
$arrayElements = $argv[1];
}
// otherwise, they are hard coded
else {
$arrayElements = 7000;
}
if ($argv[2]) {
$variability = $argv[2];
}
else {
$variability = 3;
}
// initialize arrays
$myArray = array();
$sortedArray = array();
// instantiate object and make an array
$p = new makeNumericArray($arrayElements, $variability);
$p->makeArray();
// set local array to the one set by the array making object method
$myArray = $p->getfinalArray();
if ($debug) print_r($myArray); // if debugging, check array values
// sort the array
$sortedArray = bubbleSort($myArray);
// print it for fun. Success!
print_r($sortedArray);
?>