婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼(附四個(gè)實(shí)例代碼)

php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼(附四個(gè)實(shí)例代碼)

熱門標(biāo)簽:南寧電話外呼系統(tǒng)線路 重慶外呼電銷系統(tǒng)多少錢 正規(guī)電銷機(jī)器人系統(tǒng) 南京3D地圖標(biāo)注 辦理400電話哪家好點(diǎn) 濟(jì)源百應(yīng)電銷機(jī)器人聯(lián)系方式 咸陽(yáng)電銷 邢臺(tái)400電話辦理 嘟嘟云外呼系統(tǒng)

計(jì)數(shù)排序只適合使用在鍵的變化不大于元素總數(shù)的情況下。它通常用作另一種排序算法(基數(shù)排序)的子程序,這樣可以有效地處理更大的鍵。

總之,計(jì)數(shù)排序是一種穩(wěn)定的線性時(shí)間排序算法。計(jì)數(shù)排序使用一個(gè)額外的數(shù)組C ,其中第i個(gè)元素是待排序數(shù)組 A中值等于 i的元素的個(gè)數(shù)。然后根據(jù)數(shù)組C 來(lái)將A中的元素排到正確的位置。

通常計(jì)數(shù)排序算法的實(shí)現(xiàn)步驟思路是:

1.找出待排序的數(shù)組中最大和最小的元素;

2.統(tǒng)計(jì)數(shù)組中每個(gè)值為i的元素出現(xiàn)的次數(shù),存入數(shù)組C的第i項(xiàng);

3.對(duì)所有的計(jì)數(shù)累加(從C中的第一個(gè)元素開始,每一項(xiàng)和前一項(xiàng)相加);

4.反向填充目標(biāo)數(shù)組:將每個(gè)元素i放在新數(shù)組的第C[i]項(xiàng),每放一個(gè)元素就將C[i]減去1。

PHP計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼示例如下:

?php
function counting_sort($my_array, $min, $max)
{
  $count = array();
  for($i = $min; $i = $max; $i++)
  {
    $count[$i] = 0;
  }
 
  foreach($my_array as $number)
  {
    $count[$number]++;
  }
  $z = 0;
  for($i = $min; $i = $max; $i++) {
    while( $count[$i]-- > 0 ) {
      $my_array[$z++] = $i;
    }
  }
  return $my_array;
}
$test_array = array(3, 0, 2, 5, -1, 4, 1);
echo "原始數(shù)組 :\n";
echo implode(', ',$test_array );
echo "\n排序后數(shù)組\n:";
echo implode(', ',counting_sort($test_array, -1, 5)). PHP_EOL;

輸出:

原始數(shù)組 : 3, 0, 2, 5, -1, 4, 1

排序后數(shù)組 :-1, 0, 1, 2, 3, 4, 5

下面補(bǔ)充一個(gè)例子

1、計(jì)數(shù)排序只適用于整數(shù)在小范圍內(nèi)排序

?php
$arr = [95,94,91,98,99,90,99,93,91,92];
function countSort($arr){
$max = $arr[0];
$min = $arr[0];
for($i=0;$icount($arr);$i++){
if($arr[$i]>$max){
$max = $arr[$i];
}
if($arr[$i]  $min){
$min = $arr[$i];
}
}
try{
$frequency = new SplFixedArray($max-$min+1);
for($i=0;$icount($arr);$i++){
if(empty($frequency[$arr[$i]-$min]))
$frequency[$arr[$i]-$min] = 0;
$frequency[$arr[$i]-$min] += 1;
}

$sum = 0;
for ($i=0; $i  count($frequency); $i++) {
$sum += $frequency[$i];
$frequency[$i] = $sum;
}

$splfixed = new SplFixedArray(count($arr));

for($i=(count($arr)-1);$i>=0;$i--){
$splfixed[$frequency[$arr[$i]-$min]-1] = $arr[$i];
$frequency[$arr[$i]-$min] -= 1;
}
}catch(RuntimeException $re){
echo "RuntimeException: ".$re->getMessage()."\n";
}
print_r($splfixed->toArray());
}
countSort($arr);
?>

輸出

Array
(
    [0] => 90
    [1] => 91
    [2] => 91
    [3] => 92
    [4] => 93
    [5] => 94
    [6] => 95
    [7] => 98
    [8] => 99
    [9] => 99
)

2、php計(jì)數(shù)排序

獲取序列中的最小值min和最大值max O(n)
統(tǒng)計(jì)min - max之間所有值在序列中的出現(xiàn)次數(shù) O(n)
順序輸出min - max的所有值,次數(shù)為0不輸出,其余次數(shù)為多少就輸出多少 O(k) k為數(shù)據(jù)范圍

例如序列為: 2, 4, 6, 9, 4, 8

min = 2, max = 9, n為6,k為8

統(tǒng)計(jì)出現(xiàn)次數(shù)為

[2 => 1, 3 => 0, 4 => 2, 5 => 0, 6 => 1, 7 => 0, 8 => 1, 9 => 1]

輸出結(jié)果為

2, 4, 4, 6, 8, 9

很明顯,計(jì)數(shù)排序的復(fù)雜度為O(n) + O(k),也就是和數(shù)據(jù)量和數(shù)據(jù)范圍有關(guān).
若n和k相近,則可認(rèn)為是O(n)
同時(shí),因?yàn)橐y(tǒng)計(jì)出現(xiàn)次數(shù),如果數(shù)據(jù)范圍過(guò)大而數(shù)據(jù)又很稀疏,造成的空間浪費(fèi)比較大

class CountSort
{
  private $originalData = [];
  private $rangeMap = [];
  private $resultData = [];

  public function __construct($original = [])
  {
    $this->originalData = $original;
  }

  public function sort()
  {
    list($min, $max) = $this->calculateDataRange();
    $this->statisticNumberOfOccurrence($min, $max);
    $this->resultData = $this->generateResult();
    return $this->resultData;
  }

  protected function calculateDataRange()
  {
    $max = null;
    $min = null;

    foreach ($this->originalData as $value) {
      if (!is_null($max)) {
        if ($value > $max) {
          $max = $value;
        }
      } else {
        $max = $value;
      }

      if (!is_null($min)) {
        if ($value  $min) {
          $min = $value;
        }
      } else {
        $min = $value;
      }
    }

    return [$min, $max];
  }

  protected function statisticNumberOfOccurrence($min, $max)
  {
    for ($i = $min; $i = $max; $i++) {
      $this->rangeMap[$i] = 0;
    }

    foreach ($this->originalData as $value) {
      $this->rangeMap[$value]++;
    }
  }

  protected function generateResult()
  {
    $result = [];

    foreach ($this->rangeMap as $key => $value) {
      if ($value != 0) {
        for ($i = 0; $i  $value; $i++) {
          array_push($result, $key);
        }
      }
    }
    return $result;
  }
}

$testData = [2, 3, 4, 3, 10, 30, 20, 15, 10, 12, 33];

$countSort = new CountSort($testData);
echo 'pre>';
var_dump($countSort->sort());

輸出

pre>array(11) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(10)
  [5]=>
  int(10)
  [6]=>
  int(12)
  [7]=>
  int(15)
  [8]=>
  int(20)
  [9]=>
  int(30)
  [10]=>
  int(33)
}

到此這篇關(guān)于php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)php計(jì)數(shù)排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • java 實(shí)現(xiàn)計(jì)數(shù)排序和桶排序?qū)嵗a
  • 10個(gè)python3常用排序算法詳細(xì)說(shuō)明與實(shí)例(快速排序,冒泡排序,桶排序,基數(shù)排序,堆排序,希爾排序,歸并排序,計(jì)數(shù)排序)
  • js 計(jì)數(shù)排序的實(shí)現(xiàn)示例(升級(jí)版)
  • python實(shí)現(xiàn)計(jì)數(shù)排序與桶排序?qū)嵗a
  • JAVA十大排序算法之計(jì)數(shù)排序詳解

標(biāo)簽:唐山 通遼 黃山 平頂山 河南 武漢 隴南 南通

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼(附四個(gè)實(shí)例代碼)》,本文關(guān)鍵詞  php,計(jì)數(shù),排序,算法,的,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼(附四個(gè)實(shí)例代碼)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼(附四個(gè)實(shí)例代碼)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 宁远县| 家居| 耿马| 天台县| 刚察县| 房山区| 阿拉尔市| 永登县| 洞头县| 长岛县| 开封县| 中宁县| 突泉县| 武川县| 县级市| 化州市| 九江县| 梁山县| 明光市| 富顺县| 镇沅| 宜丰县| 天台县| 宁晋县| 平昌县| 永泰县| 岳普湖县| 措美县| 扎赉特旗| 项城市| 闻喜县| 英德市| 徐汇区| 洪雅县| 鄂托克旗| 汝南县| 溧阳市| 桐城市| 亳州市| 焦作市| 宜阳县|