Пример #1
0
function main()
{
    $array = array(4, 3, 5, 2, 1);
    mostrarArray($array);
    $array = bubbleSort($array);
    mostrarArray($array);
}
Пример #2
0
/**
 * 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;
}
Пример #3
0
<?php

$arr = array(7, 3, 9, 6, 5, 1, 2, 0, 8, 4);
$sortedArr = bubbleSort($arr);
echo "<pre>";
print_r($sortedArr);
echo "</pre>";
function bubbleSort(array $arr)
{
    $sorted = false;
    while (false === $sorted) {
        $sorted = true;
        for ($i = 0; $i < count($arr) - 1; $i++) {
            $current = $arr[$i];
            $next = $arr[$i + 1];
            if ($next < $current) {
                $arr[$i] = $next;
                $arr[$i + 1] = $current;
                $sorted = false;
            }
        }
    }
    return $arr;
}
Пример #4
0
<?php

$numbers = array();
for ($i = 0; $i < 50; $i++) {
    array_push($numbers, rand(1, 1000));
}
$sortedArr = bubbleSort($numbers);
var_dump($sortedArr);
function bubbleSort($arr)
{
    $sorted = false;
    while (false === $sorted) {
        $sorted = true;
        for ($i = 0; $i < count($arr) - 1; ++$i) {
            $current = $arr[$i];
            $next = $arr[$i + 1];
            if ($next < $current) {
                $arr[$i] = $next;
                $arr[$i + 1] = $current;
                $sorted = false;
            }
        }
    }
    return $arr;
}
Пример #5
0
    $iteration = 0;
    for ($j = 0; $j < count($arr); $j++) {
        for ($i = 0; $i < count($arr) - $iteration; $i++) {
            if ($i + 1 != count($arr) - $iteration) {
                if ($arr[$i] > $arr[$i + 1]) {
                    $temp = $arr[$i];
                    $arr[$i] = $arr[$i + 1];
                    $arr[$i + 1] = $temp;
                }
            }
        }
        $iteration++;
    }
    return $arr;
}
//TODO:  Bubble sort using recursion
// function recursiveBubbleSort($arr, $start, $end) {
// 	if($start > $end) {
// 		//we're done!
// 	}
// 	if($start == $end -1 ) {
// 		//iterate to the next case
// 		recursiveBubbleSort($arr, 0, $end-1);
// 	} elseif($arr[$start]) {
// 	}
// }
$arr = array();
$arr = populateArray($arr);
echo var_dump($arr);
$arr = bubbleSort($arr);
echo var_dump($arr);
Пример #6
0
$otherList[1] = 1;
$otherList[2] = 2;
function swap(&$numbers, $i)
{
    $storage = $numbers[$i];
    $numbers[$i] = $numbers[$i - 1];
    $numbers[$i - 1] = $storage;
}
function bubbleSort($numbers)
{
    $length = sizeOf($numbers);
    $storage = 0;
    $previousIndex = 0;
    $swapped = true;
    $i = 0;
    $counter = 0;
    while ($counter < $length) {
        for ($i = 1; $i < $length; $i++) {
            $swapped = false;
            if ($numbers[$i] < $numbers[$i - 1]) {
                swap($numbers, $i);
                $swapped = true;
            }
        }
        $counter++;
    }
    return $numbers;
}
print_r(bubbleSort($otherList));
print_r(bubbleSort($numberList));
<?php

function bubbleSort(&$a)
{
    for ($posElOrd1 = count($a) - 2; $posElOrd1 > 0; $posElOrd1--) {
        for ($i = 0; $i <= $posElOrd1; $i++) {
            if ($a[$i] > $a[$i + 1]) {
                $appoggio = $a[$i];
                $a[$i] = $a[$i + 1];
                $a[$i + 1] = $appoggio;
            }
        }
        print_r($a);
    }
}
$a = array(5, 7, 1, 3, 9);
bubbleSort($a);
print_r($a);
Пример #8
0
             for ($j = $i + 1; $j < $cnt; $j++) {
                 if ($numbers[$j] < $numbers[$i]) {
                     //执行交换
                     $temp = $numbers[$i];
                     $numbers[$i] = $numbers[$j];
                     $numbers[$j] = $temp;
                 }
             }
         }
         return $numbers;
     }
     $num = array($text0, $text1, $text2, $text3, $text4, $text5, $text6, $text7, $text8);
     if ($text0 == null || $text1 == null) {
         echo "<script>" . "alert('你没有提交数据,请确定输入数据后再提交')" . "</script>";
     } else {
         var_dump(bubbleSort($num));
         echo "<br><br>" . "<a href=' maopao.html'>" . "查看源代码" . "</a>";
         echo "</p>" . "<table width='650' border='1' bgcolor='#FFFFFF' cellspacing='0'>" . "<font size='2'>" . "<th>" . "原理:比较相邻的两个数,如果第一个比第二个大,就交换他们两个。最后的元素将是最大的一个数。" . "</th\t>" . "</font>" . "</table>";
     }
 } else {
     if ($choose == "快速排序") {
         function quicksort($str)
         {
             if (count($str) <= 1) {
                 return $str;
             }
             //如果个数不大于一,直接返回
             $key = $str[0];
             //取一个值,稍后用来比较;
             $left_arr = array();
             $right_arr = array();
Пример #9
0
function createArray()
{
    $arr = array();
    for ($x = 0; $x < 100; $x++) {
        array_push($arr, rand(0, 10000));
    }
    return $arr;
}
function bubbleSort($arr)
{
    $reverseCount = count($arr);
    // 3
    while ($reverseCount != 0) {
        for ($x = 0; $x < $reverseCount; $x++) {
            if ($x != $reverseCount - 1 && $arr[$x] > $arr[$x + 1]) {
                $temp = $arr[$x];
                $arr[$x] = $arr[$x + 1];
                $arr[$x + 1] = $temp;
            }
        }
        $reverseCount--;
    }
    var_dump($arr);
}
$myArray = createArray();
$time_start = microtime();
bubbleSort($myArray);
$time_end = microtime();
$time = $time_end - $time_start;
echo "Took " . $time . " seconds";
Пример #10
0
 * Date: 10/8/2015
 * Time: 7:32 PM
 *
 */
/**
 * @param $items
 *
 * @return mixed
 */
function bubbleSort($items)
{
    $size = count($items);
    for ($i = 0; $i < $size; $i++) {
        for ($j = 0; $j < $size - 1 - $i; $j++) {
            if ($items[$j + 1] < $items[$j]) {
                arraySwap($items, $j, $j + 1);
            }
        }
    }
    return $items;
}
function arraySwap(&$arr, $index1, $index2)
{
    list($arr[$index1], $arr[$index2]) = [$arr[$index2], $arr[$index1]];
}
$arr = [7, 3, 9, 6, 5, 1, 2, 0, 8, 4];
echo 'Unsorted list:<br/>';
var_dump($arr);
echo '<b>Bubble Sorted List: </b><br>';
var_dump(bubbleSort($arr));
echo '<br><b>' . $sorted;
<?php

require_once 'verifica_ord_r.php';
function bubbleSort(&$a)
{
    for ($posElementoDaInserire = count($a) - 2; $posElementoDaInserire >= 0; $posElementoDaInserire--) {
        echo "Inizio iterazione n. " . (count($a) - $posElementoDaInserire - 1) . "<br>\n";
        print_r($a);
        for ($i = 0; $i <= $posElementoDaInserire; $i++) {
            if ($a[$i] > $a[$i + 1]) {
                // Scambio
                $appoggio = $a[$i];
                $a[$i] = $a[$i + 1];
                $a[$i + 1] = $appoggio;
            }
        }
        echo "Fine iterazione n. " . (count($a) - $posElementoDaInserire - 1) . "<br>\n";
        print_r($a);
    }
}
$vettore = array(3, 7, 5, 1, 4);
stampaOrdinato($vettore);
print_r($vettore);
bubbleSort($vettore);
stampaOrdinato($vettore);
Пример #12
0
$sortedArrs = array();
$labelM = "";
$invalidErr = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $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:";
        }
    }
Пример #13
0
<?php

$list = [5, 4, 1, 7, 9, 2, 0, 3, 6, 8];
function bubbleSort(&$list)
{
    $length = count($list);
    $sorted = $length - 1;
    for ($i = 0; $i < $length; $i++) {
        for ($j = 0; $j < $length; $j++) {
            $nextIndex = $j + 1;
            if (array_key_exists($nextIndex, $list) && $list[$j] > $list[$nextIndex]) {
                $tmp = $list[$j];
                $list[$j] = $list[$nextIndex];
                $list[$nextIndex] = $tmp;
                if ($j === $sorted) {
                    $sorted--;
                    break;
                }
            }
        }
    }
}
bubbleSort($list);
Пример #14
0
{
    $temp = $array[$index];
    $array[$index] = $array[$index + 1];
    $array[$index + 1] = $temp;
}
function bubbleSort(&$array)
{
    do {
        $swapped = false;
        for ($i = 0, $c = count($array) - 1; $i < $c; $i++) {
            if ($array[$i] > $array[$i + 1]) {
                swap($array, $i);
                $swapped = true;
            }
        }
    } while ($swapped);
}
foreach ($array as $row) {
    foreach ($row as $value) {
        $newArray[] = $value;
    }
}
bubbleSort($newArray);
$array = [];
for ($i = 0; $i < $rows; $i++) {
    $array[] = [];
    for ($j = 0; $j < $cols; $j++) {
        $array[$i][$j] = array_shift($newArray);
    }
}
print_r($array);
Пример #15
0
function testArrayData()
{
    $aData = '';
    $aData = array(3, 2, 4, 1, 6, 0);
    responseArray($aData, '原来顺序');
    responseArray(selectSort($aData), '选择排序');
    responseArray(quickSort($aData), '快速排序');
    responseArray(insertSort($aData), '插入排序');
    responseArray(bubbleSort($aData), '冒泡排序');
    responseArray(reQuickSort($aData), '反序排序');
    echo '<b>最 大 值:</b>' . $GLOBALS['PHPMax']($aData) . '<hr>';
    echo '<b>最 小 值:</b>' . $GLOBALS['PHPMin']($aData) . '<hr>';
}
Пример #16
0
<?php

// Optimized bubble sort
// https://en.wikipedia.org/wiki/Bubble_sort
function bubbleSort(array $data)
{
    $length = count($data);
    $swap = function (array &$data, $i1, $i2) {
        $tmp = $data[$i1];
        $data[$i1] = $data[$i2];
        $data[$i2] = $tmp;
    };
    for ($i = 0; $i < $length; $i++) {
        $n = 0;
        for ($j = 0; $j < $length; $j++) {
            if (isset($data[$j + 1]) && $data[$j] > $data[$j + 1]) {
                $swap($data, $j, $j + 1);
                $n += 1;
            }
        }
        if (!$n) {
            break;
        }
    }
    return $data;
}
var_dump(bubbleSort([7, 3, 6, 4, 9, 0, 1, 5, 2, 8]));
Пример #17
0
    $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 = bubbleSort($unsortedArray);
        echo "<br/>Output : " . implode(" ", $sorted);
    } else {
        echo $error;
    }
}
function bubbleSort($array)
{
    $n = count($array);
    for ($i = 0; $i < $n; $i++) {
        for ($j = $n - 1; $j > $i; $j--) {
            if ($array[$j] < $array[$j - 1]) {
                $temp = $array[$j - 1];
                $array[$j - 1] = $array[$j];
                $array[$j] = $temp;
            }
Пример #18
-10
function bubbleSort2DArray($arr)
{
    function transformArray($arr)
    {
        $len = count($arr);
        $result = [];
        for ($i = 0; $i < $len; $i++) {
            $len1 = count($arr[$i]);
            for ($j = 0; $j < $len1; $j++) {
                array_push($result, $arr[$i][$j]);
            }
        }
        return $result;
    }
    function bubbleSort($arr)
    {
        $len = count($arr);
        for ($i = 0; $i < $len; $i++) {
            for ($j = 0; $j < $len - 1 - $i; $j++) {
                if ($arr[$j] > $arr[$j + 1]) {
                    $temp = $arr[$j];
                    $arr[$j] = $arr[$j + 1];
                    $arr[$j + 1] = $temp;
                }
            }
        }
        return $arr;
    }
    function create2DArray($arr)
    {
        $arr2 = [];
        $len = count($arr);
        $arr3 = [];
        $counter = 0;
        for ($i = 0; $i < $len; $i++) {
            $arr3[$counter] = $arr[$i];
            $counter++;
            if (($i + 1) % 4 == 0) {
                array_push($arr2, $arr3);
                $arr3 = [];
                $counter = 0;
            }
        }
        return $arr2;
    }
    return create2DArray(bubbleSort(transformArray($arr)));
}