예제 #1
0
/**
 * @param array $array
 * @return array
 */
function bubble_sort(array $array)
{
    $is_sorted = false;
    $checks = 0;
    $array_length = sizeof($array);
    for ($i = 0; $i < $array_length - 1; $i++) {
        if ($array[$i] <= $array[$i + 1]) {
            $checks++;
        }
        if ($array[$i] > $array[$i + 1]) {
            $temp = $array[$i + 1];
            $array[$i + 1] = $array[$i];
            $array[$i] = $temp;
        }
    }
    if ($checks == $array_length - 1) {
        $is_sorted = true;
    }
    if ($is_sorted) {
        return $array;
    } else {
        return bubble_sort($array);
    }
}
예제 #2
0
<?php

#
# 冒泡排序
#
function bubble_sort($arr)
{
    $n = count($arr);
    for ($i = 0; $i < $n - 1; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($arr[$j] < $arr[$i]) {
                $temp = $arr[$i];
                $arr[$i] = $arr[$j];
                $arr[$j] = $temp;
            }
        }
    }
    return $arr;
}
$arr = [2, 1, 4, 5, 3];
print_r(bubble_sort($arr));
예제 #3
0
    $count = count($array);
    if ($count <= 0) {
        return false;
    }
    for ($i = 0; $i < $count; $i++) {
        for ($j = 0; $j < $count - 1; $j++) {
            if ($array[$j] > $array[$j + 1]) {
                $tmp = $array[$j + 1];
                $array[$j + 1] = $array[$j];
                $array[$j] = $tmp;
            }
        }
    }
    return $array;
}
$array = array();
for ($i = 0; $i < 20; $i++) {
    $arr = rand(1000, 10000);
    if (!in_array($arr, $array)) {
        $array[] = $arr;
    } else {
        $i--;
    }
}
$arr = bubble_sort($array);
echo '<pre>';
print_r($arr);
?>


예제 #4
0
<?php

function factorial($n)
{
    return $n == 1 ? 1 : $n * factorial($n - 1);
}
echo factorial(10) . "\n";
require_once 'b.php';
$data = array(4, 6, 1, 3, 8, 10, 100, 445, 6, 34, 4, 9);
var_dump($data) . "\n\n";
bubble_sort($data, true);
var_dump($data) . "\n\n";
/**
 * Classic Algorithms 02 - Bubble sort - 
 * 
 * @param {array} $array the array of integers we want to sort
 * @return {array} the given array sorted
 */
function bubble_sort($array)
{
    $length = count($array);
    for ($i = 1; $i < $length; $i++) {
        for ($j = 0; $j < $length - 1; $j++) {
            if ($array[$j] > $array[$j + 1]) {
                $aux = $array[$j];
                $array[$j] = $array[$j + 1];
                $array[$j + 1] = $aux;
            }
        }
    }
    return $array;
}
$array = [2, 3, 1, 6, 7, 4, 9, 11, 3, 2, 6, 8, 20, 14, 32, 1, 15, 3, 8, 9];
$sorted = bubble_sort($array);
echo "This is the original array ";
foreach ($array as $number) {
    echo "{$number}, ";
}
// sorted 1, 1, 2, 2, 3, 3, 3, 4, 6, 6, 7, 8, 8, 9, 9, 11, 14, 15, 20, 32,
echo "<br>This is the sorted array ";
foreach ($sorted as $number) {
    echo "{$number}, ";
}
예제 #6
0
<?php

function bubble_sort($array)
{
    $len = count($array);
    $j = $len - 1;
    while ($j > 0) {
        for ($i = 1; $i <= $j; $i++) {
            if ($array[$i - 1] > $array[$i]) {
                $tmp = $array[$i];
                $array[$i] = $array[$i - 1];
                $array[$i - 1] = $tmp;
            }
        }
        $j--;
    }
    return $array;
}
$x = array();
for ($i = 0; $i < 1000; $i++) {
    array_push($x, rand(1, 10000));
}
$start = microtime(true);
$y = bubble_sort($x);
$end = microtime(true);
$time_used = $end - $start;
echo "time used to sort: {$time_used} seconds<br>";
var_dump($x);
var_dump($y);
예제 #7
0
	/**
	 * 挑战记录
	 */
	public function challengelog() {
		$challengeList = null;
		$openid = $this->session->userdata ( 'openid' ) ;
		$challengeList = $this->challenge->getChallengeLogByOpenid ( $openid );
		foreach ( $challengeList as $k => &$cl ) {
			if ($cl->fopenid == null) {
				unset ( $challengeList [$k] );
				continue;
			}
			$cl->desc = intval ( $cl->result ) < 0 ? '挑战失败' : '挑战成功';
		}
		
		$challengedList = $this->challenge->getChallengeLogByFopenid ( $openid );
		foreach ( $challengedList as $k => &$cdl ) {
			$account = $this->account->getInfoByOpenid($cdl->openid);
			$cdl->fnickname = $account->nickname;
			$cdl->fopenid = $account->openid;
			$cdl->desc = intval ( $cdl->result ) < 0 ? '给你跪了' : '超越了你';
			$cdl->challenged = 1;
		}
		
		$challengeList = bubble_sort( array_merge($challengedList, $challengeList), 'create_time' );
		$this->format ( 'success', $challengeList, 0 );
	}
예제 #8
0
 public function revision($id = "")
 {
     $changed = array();
     $data = array();
     $sql = "select `revisions`.* from `revisions` where `revisions`.`id`='" . mysql_real_escape_string($id) . "' and `revisions`.`table`='investment_orgs' ";
     $q = $this->db->query($sql);
     $revision = $q->result_array();
     $revision = $revision[0];
     if ($revision['id']) {
         $investment_org_id = $revision['ipc_id'];
         $investment_org = json_decode($revision['json_data']);
         $investment_org = objectToArray($investment_org);
         $sql = "select * from `investment_orgs` where `id`=" . $this->db->escape($investment_org_id);
         $q = $this->db->query($sql);
         $investment_orgtemp = $q->result_array();
         $sql = "select * from `web_users` where `id`=" . $this->db->escape($revision['web_user_id']);
         $q = $this->db->query($sql);
         $web_user = $q->result_array();
         $web_user = $web_user[0];
         $web_user = getWebUser($web_user);
         $data['web_user'] = $web_user;
     }
     if ($investment_orgtemp[0]['id'] && $web_user) {
         $corig = $this->edit($investment_orgtemp[0]['id'], true);
         //get original data
         $data['investment_orgorig'] = $investment_orgtemp[0];
         $data['revision'] = $revision;
         $categories = $corig['categories'];
         $funding_rounds = $corig['funding_rounds'];
         $currencies = $corig['currencies'];
         $countries = $corig['countries'];
         $milestones = $corig['milestones'];
         $competitors = $corig['competitors'];
         $data['categories'] = $categories;
         $data['funding_rounds'] = $funding_rounds;
         $data['currencies'] = $currencies;
         $data['countries'] = $countries;
         $data['milestones'] = $milestones;
         $data['competitors'] = $competitors;
         if (arrDiff($corig['competitors'], $data['competitors'])) {
             $changed[] = 'competitors';
         }
         $people = array();
         if (is_array($investment_org['p_ids'])) {
             foreach ($investment_org['p_ids'] as $key => $value) {
                 $sql = "select * from `people` where `id`='" . mysql_real_escape_string($value) . "'";
                 $q = $this->db->query($sql);
                 $p = $q->result_array();
                 $p = $p[0];
                 $person = array();
                 $person['investment_org_id'] = $investment_org_id;
                 $person['person_id'] = $value;
                 $person['role'] = $investment_org['p_roles'][$key];
                 $person['start_date'] = $investment_org['p_start_dates'][$key];
                 $person['start_date_ts'] = strtotime($investment_org['p_start_dates'][$key]) . "";
                 $person['end_date'] = $investment_org['p_end_dates'][$key];
                 $person['end_date_ts'] = strtotime($investment_org['p_end_dates'][$key]) + 0 . "";
                 $person['name'] = $p['name'];
                 //$person['slug'] = $p['slug'];
                 $people[] = $person;
             }
         }
         $data['people'] = $people;
         //remove ids and edn_date_ts2
         foreach ($corig['people'] as $key => $value) {
             unset($corig['people'][$key]['id']);
             unset($corig['people'][$key]['end_date_ts2']);
         }
         $a = bubble_sort($data['people'], "name", "asc", true);
         $b = bubble_sort($corig['people'], "name", "asc", true);
         if (arrDiff($a, $b)) {
             $changed[] = "people";
         }
         //remove arrays
         $unsets = array();
         foreach ($investment_org as $key => $value) {
             if (is_array($value)) {
                 $unsets[] = $key;
             } else {
                 if (trim($value) != trim($corig['investment_org'][$key])) {
                     $changed[] = $key;
                 }
             }
         }
         foreach ($unsets as $key) {
             unset($investment_org[$key]);
         }
         $data['investment_org'] = $investment_org;
         $data['changed'] = $changed;
         $data['content'] = $this->load->view('investment_orgs/add', $data, true);
         $this->load->view('layout/main', $data);
     }
 }
 * direct pseudo-code implementation from Cormen et al book, Introduction
 * to Algorithms.
 *
 * Copyright (c) 2012 Akeda Bagus (admin@gedex.web.id).
 *
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * license.
 */
function bubble_sort(&$elements, $fn = 'comparison_function')
{
    for ($i = 0; $i < sizeof($elements) - 1; $i++) {
        for ($j = sizeof($elements) - 1; $j >= $i + 1; $j--) {
            if ($fn($elements[$j], $elements[$j - 1])) {
                // swap the element
                $elements[$j] ^= $elements[$j - 1];
                $elements[$j - 1] ^= $elements[$j];
                $elements[$j] ^= $elements[$j - 1];
            }
        }
    }
}
function comparison_function(&$a, &$b)
{
    return $a < $b;
}
// Example usage:
$a = array(3, 5, 9, 8, 5, 7, 2, 1, 13);
var_dump($a);
bubble_sort($a);
// Sort the elements
var_dump($a);
예제 #10
0
<?php

$input = array(6, 5, 3, 1, 8, 7, 2, 4);
function bubble_sort($arr)
{
    $length = count($arr);
    for ($i = 0; $i < $length; $i++) {
        for ($j = $length - 1; $j > $i; $j--) {
            if ($arr[$j] < $arr[$j - 1]) {
                $t = $arr[$j];
                $arr[$j] = $arr[$j - 1];
                $arr[$j - 1] = $t;
            }
        }
    }
    return $arr;
}
// 1, 2, 3, 4, 5, 6, 7, 8
$output = bubble_sort($input);
 public function testNoParameters()
 {
     $this->assertEmpty(bubble_sort([]));
 }
예제 #12
0
파일: 算法.php 프로젝트: akaiidle/newborn
<?php

$arr = array(28, 43, 54, 62, 21, 66, 32, 78, 36, 76, 39);
$res = bubble_sort($arr);
var_dump($res);
$res = quick_sort($arr);
var_dump($res);
$hs = half_search($arr, 78);
var_dump($hs);
//ðÅÝÅÅÐò
function bubble_sort($arr)
{
    if (empty($arr) || !is_array($arr)) {
        return $arr;
    }
    $count = count($arr);
    if ($count < 2) {
        return $arr;
    }
    for ($i = $count; $i > 0; $i--) {
        for ($j = 0; $j < $i; $j++) {
            if ($arr[$j] < $arr[$j - 1]) {
                $temp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $temp;
            }
        }
    }
    return $arr;
}
//¿ìËÙÅÅÐò
예제 #13
0
    list($array[$id], $array[$id2]) = array($array[$id2], $array[$id]);
}
//Declare the Bubble Sort Function
function bubble_sort($array)
{
    $size = sizeof($array);
    for ($i = 0; $i < $size; $i++) {
        for ($j = 0; $j < $size; $j++) {
            if ($array[$i] < $array[$j]) {
                $temp = $array[$i];
                $array[$i] = $array[$j];
                $array[$j] = $temp;
            }
        }
    }
    return $array;
}
//Microtime function to set start time
$time_start = microtime(true);
//Printing the Array with commas
echo implode(", ", bubble_sort($sample_array));
//Stopping Microtime clock
$time_end = microtime(true);
$time = $time_end - $time_start;
//Printing result of Microtime function
echo "<br><br>Did sort in " . $time . " seconds\n";
?>
	</p>
		</div>
	</body>
</html>
예제 #14
0
    if (is_array($array_to_sort) && count($array_to_sort) > 1) {
        for ($i = 0; $i < count($array_to_sort); $i++) {
            for ($j = $i; $j < count($array_to_sort); $j++) {
                if ($array_to_sort[$i] > $array_to_sort[$j]) {
                    $array_to_sort[$i] = $array_to_sort[$i] + $array_to_sort[$j];
                    $array_to_sort[$j] = $array_to_sort[$i] - $array_to_sort[$j];
                    $array_to_sort[$i] = $array_to_sort[$i] - $array_to_sort[$j];
                }
            }
        }
        return $array_to_sort;
    } else {
        return "Pass an Valid input (only array() with size greater than 2)";
    }
}
r_print(bubble_sort($array_to_sort));
//////////////////
//Insertion Sort//
//////////////////
function insertion_sort($array_to_sort = '')
{
    $s = [];
    for ($i = 0; $i < count($array_to_sort); $i++) {
        if (count($s) == 0) {
            array_push($s, $array_to_sort[$i]);
            continue;
        } else {
            $flag = false;
            for ($j = count($s) - 1; $j >= 0; $j--) {
                if ($array_to_sort[$i] < $s[$j]) {
                    $temp = $s[$j];
<?php

/**
 * @file bubble_sort.php
 * @author lurenzhong@baidu.com
 * @date 16/3/1
 * @brief 冒泡排序
 * @version
 */
function bubble_sort($arr)
{
    $len = count($arr);
    if ($len <= 1) {
        return $arr;
    }
    for ($i = 1; $i < $len; $i++) {
        //比较轮数 n-1轮
        for ($j = 0; $j < $len - $i; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                //交换过程
                $tmp = $arr[$j + 1];
                $arr[$j + 1] = $arr[$j];
                $arr[$j] = $tmp;
            }
        }
    }
    return $arr;
}
$arr = array(111, 3, 4, 55, 12, 0, 34);
$sort_arr = bubble_sort($arr);
print_r($sort_arr);
예제 #16
0
function swap($index1, $index2, &$arr)
{
    $temp = $arr[$index1];
    $arr[$index1] = $arr[$index2];
    $arr[$index2] = $temp;
}
function bubble_sort($arr)
{
    $time_start = microtime(true);
    for ($i = 0; $i < count($arr); $i++) {
        for ($j = 0; $j < count($arr) - $i - 1; $j++) {
            if ($arr[$j] >= $arr[$j + 1]) {
                swap($j, $j + 1, $arr);
            }
        }
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "This bubble sort took: " . $time . " second(s).<br>";
    echo "The sorted array: <br>";
    var_dump($arr);
}
$numbers = array();
for ($i = 0; $i < 100; $i++) {
    $numbers[] = rand(1, 10000);
}
echo "The original array: <br>";
var_dump($numbers);
bubble_sort($numbers);
 public function testNoParameters()
 {
     $this->assertEquals(array(), bubble_sort(array()));
 }
예제 #18
0
파일: index.php 프로젝트: sourabhpal/PHP
<?php

$sample = array();
for ($i = 0; $i < 100; $i++) {
    $sample[] = rand(0, 10000);
}
function bubble_sort($array)
{
    $sorted = false;
    while ($sorted == false) {
        $sorted = true;
        for ($i = 0; $i < count($array) - 1; $i++) {
            $current_value = $array[$i];
            $next_value = $array[$i + 1];
            if ($next_value < $current_value) {
                $array[$i] = $next_value;
                $array[$i + 1] = $current_value;
                $sorted = false;
            }
        }
    }
    return $array;
}
$sorted_array = bubble_sort($sample);
var_dump($sorted_array);
function swap($arr, $i1, $i2)
{
    $temp = $arr[$i1];
    $arr[$i1] = $arr[$i2];
    $arr[$i2] = $temp;
    return $arr;
}
function bubble_sort($arr)
{
    $time_start = microtime(true);
    $x = count($arr) - 1;
    while ($x >= 0) {
        for ($i = 0; $i < count($arr) - 1; $i++) {
            $i2 = $i + 1;
            if ($arr[$i] > $arr[$i2]) {
                $arr = swap($arr, $i, $i2);
            }
        }
        $x--;
    }
    $time_stop = microtime(true);
    echo $time_stop - $time_start;
    var_dump($arr);
}
$arr = [];
for ($i = 1; $i <= 1000; $i++) {
    $arr[] = rand(1, 10000);
}
bubble_sort($arr);