コード例 #1
0
ファイル: shellSort.php プロジェクト: lcp0578/PHP-algorithm
                $temp = $list[$i];
                for ($j = $i - $gap; $j >= 0; $j -= $gap) {
                    if ($temp < $list[$j]) {
                        $list[$j + $gap] = $list[$j];
                        $list[$j] = $temp;
                    } else {
                        break;
                    }
                }
            }
        }
    }
    return $list;
}
$list = array(3, 6, 2, 4, 10, 1, 9, 8, 5, 7);
var_dump(shellSort($list));
/**
 * 第1趟, gap = 5
 * 分成了两个数组:(对应下标元素进行比较)
 * (3, 6, 2, 4, 10) (1, 9, 8, 5, 7)
 * 交换后的过程:
 * (1, ...) (3, ...)
 * (1, 6, ...) (3, 9, ...)
 * (1, 6, 2, ...) (3, 9, 8, ...)
 * (1, 6, 2, 4, ...) (3, 9, 8, 5, ...)
 * (1, 6, 2, 4, 7) (3, 9, 8, 5, 10)
 * 第2趟,gap = 2
 * 分成了五个数组
 * (1, 6) (2, 4) (7, 3) (9, 8) (5, 10)
 * 交换过程
 * (1, ...) (2, ...) (5, ...) (7, ...) (9, ...)
コード例 #2
0
ファイル: shellsort.php プロジェクト: biroa/php_algorithms
            for ($i = count($leftHand) - 1; $i >= 0; --$i) {
                if ($card >= $leftHand[$i]) {
                    for ($j = 0; $j <= $i; ++$j) {
                        $reindexedLeftHand[$j] = $leftHand[$j];
                    }
                    $reindexedLeftHand[] = $card;
                    for ($j = $i + 1; $j < count($leftHand); ++$j) {
                        $reindexedLeftHand[$j + 1] = $leftHand[$j];
                    }
                    $insertedCard = true;
                    break;
                }
            }
            if (false === $insertedCard) {
                $reindexedLeftHand[] = $card;
                foreach ($leftHand as $cardInLeftHand) {
                    $reindexedLeftHand[] = $cardInLeftHand;
                }
            }
            $leftHand = $reindexedLeftHand;
        }
    }
    return $leftHand;
}
$arr = [];
for ($i = 0; $i < 100; ++$i) {
    $arr[] = $i;
}
shuffle($arr);
$sortedArr = shellSort($arr);
var_dump($sortedArr);
コード例 #3
0
ファイル: starght.php プロジェクト: Vince--/PHP-Sorting
             for ($j = $i + $step; $j <= $lastInx; $j += $step) {
                 #待排序元素从该组第二个元素开始
                 $insertEle = $arr[$j];
                 for ($k = $i; $k < $j; $k += $step) {
                     if ($arr[$k] > $arr[$j]) {
                         $arr = move($arr, $step, $k, $j - $step);
                         $arr[$k] = $insertEle;
                         break;
                     }
                 }
             }
         }
         return $arr;
     }
     $arr = array($text0, $text1, $text2, $text3, $text4, $text5, $text6, $text7, $text8);
     $arr = shellSort($arr);
     if ($text0 == null || $text1 == null) {
         echo "<script>" . "alert('你没有提交数据,请确定输入数据后再提交')" . "</script>";
     } else {
         print_r($arr);
         echo "</p>" . "<table width='650' border='1' bgcolor='#FFFFFF' cellspacing='0'>" . "<font size='2'>" . "<th>" . "原理:把记录按下标的一定增量分组,对每组使用直接插入排序算法排序,当增量减至1时,整个文件恰被分为一组。" . "</font>" . "</table>";
     }
 } else {
     if ($choose == "冒泡排序") {
         function bubbleSort($numbers)
         {
             $cnt = count($numbers);
             for ($i = 0; $i < $cnt - 1; $i++) {
                 //循环比较
                 for ($j = $i + 1; $j < $cnt; $j++) {
                     if ($numbers[$j] < $numbers[$i]) {