-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorter_commented.php
More file actions
93 lines (81 loc) · 3.12 KB
/
sorter_commented.php
File metadata and controls
93 lines (81 loc) · 3.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
// ini_set('display_startup_errors',1);
// ini_set('display_errors',1);
// error_reporting(-1);
/*
* url format: http://dlevine.us/phptest/numsort/sorter.php?numbers=5,1,2,1,7,3
*/
//Get the numbers
$numbers = $_GET['numbers'] = isset($_GET['numbers']) ? $_GET['numbers'] : "";
//Make sure there are no spaces
$numbers = str_replace(" ", ",", $numbers);
//Explore to array
$numbers = explode(",", $numbers);
//How many items are there?
$numberslength = count($numbers);
$ordered = array();
$counter = 1; // count our position
function showDebugLog($arrayOne, $arrayTwo){
$numberslength= $GLOBALS['numberslength'];
$counter = $GLOBALS['counter'];
if(!isset($arrayOne[$counter])){$counter=0;}
echo "Input Numbers: <br>";
print_r($arrayOne);
//echo "<br><br>$arrayOne[0] compared to $arrayOne[$counter]";
echo "<br><br>Re-ordered Numbers: <br>";
print_r($arrayTwo);
echo "<!--<br><br>Remaining Numbers: " . --$numberslength . "<br>--><hr>";
}
//showDebugLog($numbers, $ordered); //Print for debugging
/*
* Compare each number to all other numbers in the array
* Push larger numbers to the end
* Push smaller numbers to the ordered array
* Push the last number to the ordered array (should be the largest)
*/
foreach ($numbers as $element) {
if (!is_numeric($element)) {
array_push($ordered, "Please enter numbers.");
$ordered = json_encode($ordered);
echo ($ordered);
exit(0);
}
}
//Make sure something is in the array
while($numberslength > 0){
//If an array only has one item just push it
if($numberslength == 1){
//Push it to ordered
array_push($ordered, $numbers[0]);
// Remove it from the input array
unset($numbers[0]);
//Evaluate input array (make sure nothing is left)
$numbers = array_values($numbers);
//Break
//If the number is not the last one
//evaluate to see if it's the smallest
} elseif($numbers[0] < $numbers[$counter]){//Is it smaller than the next number
if($counter < $numberslength-1){//If it is check to see if the next number is the last number
$counter += 1; //If there's more than 2 numbers remaining keep checking
//if this number is the smallest in the group push it to the ordered array
} else {
array_push($ordered, $numbers[0]); //Push it
unset($numbers[0]); //Remove from the original array
$numbers = array_values($numbers); //Re-evaluate the original array
$counter = 1; //Reset the counter
//showDebugLog($numbers, $ordered); //Print for debugging
}
}
else{ //If the number is NOT the smallest in the group
//move it to the end
$moveToEnd = $numbers[0]; //Store this number to a variable
unset($numbers[0]); //Remove it from the original array
array_push($numbers, $moveToEnd); //Push it to the end
$numbers = array_values($numbers); //Re-evaluate it
}
$numberslength = count($numbers); //Re-evaluate the length of numbers
}
//echo "<br> Final Order: "; print_r($ordered); echo "<br> Encode as json: ";
$ordered = json_encode($ordered);
echo ($ordered);
?>