<?php $arr = array(); for ($i = 0; $i < 100; $i++) { $arr[] = mt_rand(1, 10000); } $start_time = microtime(true); function selectionSort($array) { for ($i = 0; $i < count($array); $i++) { for ($j = $i; $j < count($array); $j++) { if ($array[$j] < $array[$i]) { $temp = $array[$j]; $array[$j] = $array[$i]; $array[$i] = $temp; } } } return $array; } $stop_time = microtime(true); echo "time took: " . ($stop_time - $start_time) . " seconds."; echo var_dump(selectionSort($arr));
for ($j = $i; $j < count($array) - $i; $j++) { if ($array[$j] < $array[$lowestIndx]) { $lowestIndx = $j; } else { if ($array[$j] > $array[$highestIndx]) { $highestIndx = $j; } } } $temp = $array[$lowestIndx]; $array[$lowestIndx] = $array[$i]; $array[$i] = $temp; if ($array[$highestIndx] == $array[$i]) { $highestIndx = $lowestIndx; } $temp = $array[$highestIndx]; $array[$highestIndx] = $array[count($array) - ($i + 1)]; $array[count($array) - ($i + 1)] = $temp; } return $array; } $arrayTest = []; for ($i = 0; $i < 10000; $i++) { array_push($arrayTest, rand(0, 10000)); } echo implode(", ", $arrayTest); echo ' '; $time_start = microtime(true); echo implode(", ", selectionSort($arrayTest)); $time_end = microtime(true); echo " " . ($time_end - $time_start);
// Sort tests $unsortedArray1 = array(0, 1, 2, 3, 4, 5, 6); $unsortedArray2 = array(7, 6, 5, 4, 3, 2, 1, 0); $unsortedArray3 = array(3, 2, 5, 1, 7, 3, 0, 2, 0, 6, 11, 3); echo "UNSORTED ARRAYS\n"; print_r($unsortedArray1); print_r($unsortedArray2); print_r($unsortedArray3); echo "INSERTION SORT ARRAYS\n"; print_r(insertionSort($unsortedArray1)); print_r(insertionSort($unsortedArray2)); print_r(insertionSort($unsortedArray3)); echo "SELECTION SORT ARRAYS\n"; print_r(selectionSort($unsortedArray1)); print_r(selectionSort($unsortedArray2)); print_r(selectionSort($unsortedArray3)); /* String Pattern Matching */ function findmatch($text, $pattern) { // Returns start position of $pattern in $text // or -1 if there is no match $textLen = strlen($text); $patternLen = strlen($pattern); for ($i = 0; $i <= $textLen - $patternLen; $i++) { $j = 0; while ($j < $patternLen && $text[$i + $j] == $pattern[$j]) { $j++; } if ($j == $patternLen) {
* Date: 10/7/2015 * Time: 7:32 PM * */ /** * @param array $arr * * @return array */ function selectionSort(array $arr) { for ($i = 0; $i < count($arr); ++$i) { $min = null; $minKey = null; for ($j = $i; $j < count($arr); ++$j) { if (null === $min || $arr[$j] < $min) { $minKey = $j; $min = $arr[$j]; } } $arr[$minKey] = $arr[$i]; $arr[$i] = $min; } return $arr; } $arr = [7, 3, 9, 6, 5, 1, 2, 0, 8, 4]; echo 'Unsorted list:<br/>'; var_dump($arr); $sortedArr = selectionSort($arr); echo 'Sorted list:<br/>'; var_dump($sortedArr);
<?php $array = [5, 10, 9, 3, 6, 6, 11, 1, 2, 5]; print_r(bubbleSort($array)); print_r(selectionSort($array)); print_r(insertionSort($array)); print_r(mergeSort($array)); print_r(quickSort($array)); /** * Sort an array and echo it * Buble sort worst case & average O(n2) * Only nice thing is can tell if list is already sorted * Not used in real world, insertion sort is better * @param array $myArray */ function bubbleSort(array $myArray) { for ($i = 0; $i < count($myArray) - 1; $i++) { $next = $i + 1; if ($myArray[$next] < $myArray[$i]) { list($myArray[$i], $myArray[$next]) = [$myArray[$next], $myArray[$i]]; return bubbleSort($myArray); } } return $myArray; } /** * In place, O(n2) * Worse than insertion sort usually * Simple * @param array $myArray
<?php function selectionSort(&$a) { for ($last_ordered = 0; $last_ordered < count($a) - 2; $last_ordered++) { // trovare posizione elemento minimo // degli elementi non ordinati $pos_min = $last_ordered; for ($j = $pos_min + 1; $j < count($a); $j++) { if ($a[$j] < $a[$pos_min]) { $pos_min = $j; } } $min = $a[$pos_min]; $a[$pos_min] = $a[$last_ordered]; $a[$last_ordered] = $min; print_r($a); } } $a = array(5, 7, 1, 3, 9); selectionSort($a); print_r($a);
<h2>Problem 4. Selection Sort</h2> </header> <section class="panel-body"> <?php echo join(" ", selectionSort(array(1, 13, 33, 44, 56, 1, 2, 312, 1, 123123))); ?> </section> </section> <section class="panel panel-info top-buffer"> <header class="panel-heading"> <h2>Problem 5. Binary Search </h2> </header> <section class="panel-body"> <?php echo join(" ", selectionSort(array(1, 13, 33, 44, 56, 1, 2, 312, 1, 123123))); ?> </section> </section> <section class="panel panel-info top-buffer"> <header class="panel-heading"> <h2>Problem 6. **Simple calculator</h2> </header> <section class="panel-body"> <?php ?> </section> </section> </div> </body>
<?php require_once 'verifica_ord_r.php'; function selectionSort(&$a) { for ($posElementoDaInserire = 0; $posElementoDaInserire < count($a) - 1; $posElementoDaInserire++) { echo "Inizio iterazione n. " . ($posElementoDaInserire + 1) . "<br>\n"; print_r($a); // Cerco la posizione dell'elemento minimo $pos_min = $posElementoDaInserire; for ($i = $pos_min + 1; $i < count($a); $i++) { if ($a[$i] < $a[$pos_min]) { $pos_min = $i; } } /* $pos_min e' l'indice dell'elemento non ancora ordinato piu' piccolo */ // Scambio $appoggio = $a[$posElementoDaInserire]; $a[$posElementoDaInserire] = $a[$pos_min]; $a[$pos_min] = $appoggio; echo "Fine iterazione n. " . ($posElementoDaInserire + 1) . "<br>\n"; print_r($a); } } $vettore = array(3, 7, 5, 1, 4); stampaOrdinato($vettore); print_r($vettore); selectionSort($vettore); stampaOrdinato($vettore);
<?php $arr = array(); $arr = populateArray($arr); $test = sort($arr); ############ Test Regular function ################## $start = microtime(true); $sorted = selectionSort($arr); $end = microtime(true); $duration = $end - $start; $correct = $test == $sorted; echo "<h2> Regular Selection Sort </h2>"; echo "<p> Duration: " . $duration . "</p>"; echo "<p> should be 1: " . $correct . "</p>"; ############ Test Recusive function ################ $start = microtime(true); $sorted = recursiveSelectionSort($arr, 0, count($arr) - 1); $end = microtime(true); $duration = $end - $start; $correct = $test == $sorted; echo "<h2> Recursive Selection Sort </h2>"; echo "<p> Duration: " . $duration . "</p>"; echo "<p> should be 1: " . $correct . "</p>"; //TODO: Add recursion function populateArray($arr) { for ($i = 0; $i < 2000; $i++) { $num = rand(0, 10000); array_push($arr, $num); } return $arr;
$numVal = $_POST["numbers"]; $sortType = $_POST["sortT"]; $invalidErr = false; if (empty($numVal) || !preg_match("/^[1-9 ][ 0-9]*\$/", $numVal)) { $invalidErr = true; } else { $labelM = ""; $invalidErr = false; $numArrs = explode(" ", $numVal); switch ($sortType) { case 0: $sortedArrs = bubbleSort($numArrs); $labelM = "sorted using bubble sort <br> RESULT:"; break; case 1: $sortedArrs = selectionSort($numArrs); $labelM = "sorted using select sort <br> RESULT:"; break; case 2: $sortedArrs = quickSort($numArrs); $labelM = "sorted using quick sort <br> RESULT:"; break; default: sort($sortedArrs); $labelM = "sorted using default php sort <br> RESULT:"; } } } function quickSort($arr) { $loe = $gt = array();