Example #1
0
/**
 * @param array $arr
 *
 * @return array
 */
function shellSort(array $arr)
{
    $gap = floor(count($arr) / 2);
    while ($gap > 0) {
        for ($i = 0; $i < count($arr) - $gap; ++$i) {
            $arrWithGapsKeys = [];
            $arrWithGaps = [];
            $loop = true;
            $j = $i;
            while ($loop) {
                if (isset($arr[$j])) {
                    $arrWithGapsKeys[] = (int) $j;
                    $arrWithGaps[] = $arr[$j];
                    $j += $gap;
                } else {
                    $loop = false;
                }
            }
            $arrWithGapsOrdered = insertionSort($arrWithGaps);
            foreach ($arrWithGapsKeys as $key) {
                $arr[$key] = current($arrWithGapsOrdered);
                next($arrWithGapsOrdered);
            }
        }
        $gap = floor($gap / 2);
    }
    return $arr;
}
Example #2
0
function insertionSort($arr, $maxIndex, $iteration)
{
    if ($iteration == $maxIndex - 1) {
        //base case: we are at the last element
        // echo "<h1>Iteration ".$iteration."</h1>";
        // echo var_dump($arr);
        return $arr;
    } else {
        $sorted = insertionSort($arr, $maxIndex, $iteration + 1);
        $element = $arr[$iteration];
        // All elements to the right of index $i are assumed to be sorted.
        // Now we just have to figure out where $el fits in the sorted array
        for ($i = $iteration + 1; $i < $maxIndex; $i++) {
            if ($element > $sorted[$i]) {
                // $el is bigger, swap so $el moves to the right in the array.
                $sorted[$i - 1] = $sorted[$i];
                $sorted[$i] = $element;
            }
        }
        // echo "<h1>Iteration ".$iteration."</h1>";
        // echo var_dump($sorted);
        return $sorted;
    }
}
Example #3
0
<?php

function insertionSort($arr, $arrLen)
{
    for ($i = 1; $i < $arrLen; $i++) {
        $current = $arr[$i];
        $j = $i - 1;
        while ($j >= 0 && $arr[$j] > $current) {
            $arr[$j + 1] = $arr[$j];
            $j--;
        }
        $arr[$j + 1] = $current;
        fputs(STDOUT, implode(" ", $arr) . "\n");
    }
}
$fp = fopen("php://stdin", "r");
fscanf($fp, "%d", $arrLen);
$arr = explode(' ', fgets($fp));
for ($i = 0; $i < $arrLen; $i++) {
    $arr[$i] = (int) $arr[$i];
}
insertionSort($arr, $arrLen);
<?php

function insertionSort($ar)
{
    $temp = $ar[sizeof($ar) - 1];
    $n = sizeof($ar) - 1;
    $t = sizeof($ar) - 1;
    for ($i = 0; $i < sizeof($ar); $i++) {
        if ($ar[$n - $i] > $temp) {
            $ar[$t] = $ar[$n - $i];
            echo implode(" ", $ar) . "\n";
        } else {
            $ar[$t] = $temp;
            echo implode(" ", $ar) . "\n";
            break;
        }
        $t = $t - 1;
    }
}
$fp = fopen("php://stdin", "r");
fscanf($fp, "%d", $m);
$ar = array();
$s = fgets($fp);
$ar = explode(" ", $s);
for ($i = 0; $i < count($ar); $ar[$i++] += 0) {
}
insertionSort($ar);
Example #5
0
<?php

$handle = fopen("input.txt", "r");
$cases = (int) fgets($handle);
for ($i = 1; $i <= $cases; $i++) {
    $n = (int) fgets($handle);
    $a = trim(fgets($handle));
    $a = explode(' ', $a);
    $a = insertionSort($a);
    echo $a . "\n";
}
function insertionSort($array)
{
    $length = count($array);
    $moves = 0;
    for ($i = 1; $i < $length; $i++) {
        $element = (int) $array[$i];
        $j = $i;
        while ($j > 0 && (int) $array[$j - 1] > $element) {
            //move value to right and key to previous smaller index
            $array[$j] = $array[$j - 1];
            $j = $j - 1;
            $moves++;
        }
        //put the element at index $j
        $array[$j] = $element;
    }
    return $moves;
}
Example #6
0
        }
    }
    return $arr;
}
// 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;
Example #7
0
<?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
    $temp = explode(".", $_FILES["userfile"]["name"]);
    $extension = end($temp);
    if ($_FILES["userfile"]["error"] > 0) {
        $error .= "Error opening the file<br />";
    }
    if (!in_array($extension, $allowedExts)) {
        $error .= "Extension not allowed<br />";
    }
    if ($_FILES["userfile"]["size"] > 102400) {
        $error .= "File size shoud be less than 100 kB<br />";
    }
    if ($error == "") {
        $section = file_get_contents($_FILES['userfile']['tmp_name']);
        echo "Input File Content: " . $section;
        $unsortedArray = explode(" ", $section);
        $sorted = insertionSort($unsortedArray);
        echo "<br/>Output : " . implode(" ", $sorted);
    } else {
        echo $error;
    }
}
function insertionSort($array)
{
    $n = count($array);
    for ($i = 1; $i < $n; $i++) {
        for ($k = $i; $k >= 1 && $array[$k] < $array[$k - 1]; $k--) {
            $temp = $array[$k - 1];
            $array[$k - 1] = $array[$k];
            $array[$k] = $temp;
        }
    }
Example #9
0
<?php

function insertionSort(array $array)
{
    $length = count($array);
    for ($i = 1; $i < $length; $i++) {
        $element = $array[$i];
        $j = $i;
        while ($j > 0 && $array[$j - 1] > $element) {
            //move value to right and key to previous smaller index
            $array[$j] = $array[$j - 1];
            $j = $j - 1;
            echo implode(' ', $array) . "\n";
        }
        //put the element at index $j
        $array[$j] = $element;
    }
    return $array;
}
insertionSort(array(2, 22, 3, 12, 3, 3, 4, 5, 1, 8, 11, 0));
<?php

$array = [41, 31, 59, 26, 41, 58];
function insertionSort($A)
{
    for ($j = 1; $j < count($A); $j++) {
        $key = $A[$j];
        $i = $j - 1;
        while ($i >= 0 && $A[$i] > $key) {
            $A[$i + 1] = $A[$i];
            $A[$i] = $key;
            $i--;
        }
    }
    return $A;
}
echo implode(insertionSort($array), ' ');
Example #11
0
 *      Vetor de números inteiros
 */
function insertionSort($vetor)
{
    //Vamos começar o loop pelo segundo elemento do vetor
    for ($i = 1; $i < count($vetor); $i++) {
        $anterior = $i - 1;
        $proximo = $anterior + 1;
        $troca = true;
        while ($troca) {
            if ($vetor[$anterior] > $vetor[$proximo]) {
                $tmp = $vetor[$anterior];
                $vetor[$anterior] = $vetor[$proximo];
                $vetor[$proximo] = $tmp;
            } else {
                $troca = false;
            }
            $anterior--;
            $proximo = $anterior + 1;
            if ($anterior < 0) {
                $troca = false;
            }
        }
    }
    return $vetor;
}
$vetor = array(98, 78, 2, 4, 100, -2);
$vetor = insertionSort($vetor);
for ($i = 0; $i < count($vetor); $i++) {
    echo $vetor[$i] . '<br>';
}
<?php

require_once 'verifica_ord_r.php';
function insertionSort(&$a)
{
    for ($posElementoDaInserire = 1; $posElementoDaInserire < count($a); $posElementoDaInserire++) {
        echo "Inizio iterazione n. {$posElementoDaInserire}<br>\n";
        print_r($a);
        if ($a[$posElementoDaInserire] >= $a[$posElementoDaInserire - 1]) {
            continue;
        }
        $elementoDaInserire = $a[$posElementoDaInserire];
        $i = $posElementoDaInserire - 1;
        while ($i >= 0 && $a[$i] > $elementoDaInserire) {
            $a[$i + 1] = $a[$i];
            $i--;
        }
        $a[$i + 1] = $elementoDaInserire;
    }
}
$vettore = array(3, 7, 5, 1, 4);
stampaOrdinato($vettore);
print_r($vettore);
insertionSort($vettore);
stampaOrdinato($vettore);
Example #13
0
 * Created by PhpStorm.
 * User: biroa
 * Date: 10/10/2015
 * Time: 4:34 PM
 *
 */
/**
 * @param array $numbers
 *
 * @return array
 */
function insertionSort(array $numbers)
{
    $count = count($numbers);
    for ($i = 1; $i < $count; $i++) {
        $j = $i - 1;
        $key = $numbers[$i];
        while ($j >= 0 && $numbers[$j] > $key) {
            $numbers[$j + 1] = $numbers[$j];
            $numbers[$j] = $key;
            $j = $j - 1;
        }
    }
    return $numbers;
}
$arr = [7, 3, 9, 6, 5, 1, 2, 0, 8, 4];
echo 'Unsorted list:<br/>';
var_dump($arr);
echo '<b>Insertion Sorted List: </b><br>';
var_dump(insertionSort($arr));
echo '<br><b>' . $sorted;