Math&php:以特殊方式快速排序数组[1..N]

$array = array(1, 2, 3, 4, 5, ..., N);

还有一个数字D = 10%.以这样的方式对数组进行排序的最快方法是什么:

$sorted_array = {a[i]} 

在混合顺序中包含$array的元素,但也包含:

abs(a[i + 1] - a[i]) >= N * 10% 

对于任何[i]并尽可能随机看.

例如,

// assume D = 25%
$array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

// so the difference between any neighbors is >= 4 = 10 * 25%.
$sorted_array = array(4, 8, 3, 7, 1, 5, 9, 2, 6, 10);

当然如果D很大,就不可能对我想要的数组进行排序.我不需要100%完美的结果,但我希望数字看起来“随机化”,并且大多数数字至少与10%不同.

我有一个奇怪的任务,但它有一个实用的区域可供使用.我想从图像中提取随机线,它们应该尽可能地不同.当然,数字图像(照片等)上的邻居线看起来非常相似.

我说得对吗?

最佳答案 我知道提供代码不是一个好主意,但我对这个问题很感兴趣.我是这样做的:

$d = 0.3;
$random = array();

// Populate the original array
for ($n=1; $n <= 10; $n++) {
    $arr[] = $n;
}

$count = count($arr);

// Loop through array
foreach (array_keys($arr) as $key) {
    if (!isset($prev_key)) {
        $prev_key = array_rand($arr);
    }
    $possibles = array(); // This stores the possible values
    echo "Trying: $prev_key";
    echo ":\n";

    // Loop through the array again and populate $possibles with all possible
    // values based on the previous values
    foreach (array_keys($arr) as $n) {
        if ($arr[$n] < $prev_key - $count * $d || $arr[$n] > $prev_key + $count * $d) {
            $possibles[] = $n;
            echo $arr[$n]." is valid\n";
        }
        else {
            echo $arr[$n];
            echo " outside range\n";
        }
    }

    // If there is nothing outside that range, just return the remaining values
    if (count($possibles) == 0) {
        $possibles = array_keys($arr);
        echo "Nothing within range so just returning whole array\n";
    }
    echo "\n";

    // Choose random value from the possible values array
    $rand_key = $possibles[array_rand($possibles)];

    $random[] = $arr[$rand_key];
    $prev_key = $arr[$rand_key];

    // Unset this value from the original array since we can only use the
    // values once
    unset($arr[$rand_key]);
}

print_r($random);

这将产生如下输出:

Trying: 8:
1 is valid
2 is valid
3 is valid
4 is valid
5 outside range
6 outside range
7 outside range
8 outside range
9 outside range
10 outside range

Trying: 2:
1 outside range
3 outside range
4 outside range
5 outside range
6 is valid
7 is valid
8 is valid
9 is valid
10 is valid

Trying: 9:
1 is valid
3 is valid
4 is valid
5 is valid
6 outside range
7 outside range
8 outside range
10 outside range

Trying: 5:
1 is valid
3 outside range
4 outside range
6 outside range
7 outside range
8 outside range
10 is valid

Trying: 10:
1 is valid
3 is valid
4 is valid
6 is valid
7 outside range
8 outside range

Trying: 4:
1 outside range
3 outside range
6 outside range
7 outside range
8 is valid

Trying: 8:
1 is valid
3 is valid
6 outside range
7 outside range

Trying: 3:
1 outside range
6 outside range
7 is valid

Trying: 7:
1 is valid
6 outside range

Trying: 1:
6 is valid

Array
(
    [0] => 2
    [1] => 9
    [2] => 5
    [3] => 10
    [4] => 4
    [5] => 8
    [6] => 3
    [7] => 7
    [8] => 1
    [9] => 6
)

唯一的缺点是,由于它随机获取行,因此有可能接近末尾的值可能不在定义的范围之外.通过我的测试,使用上述$d = 0.25和1000值,这种情况大约为4%.解决这个问题的一种方法就是将这些值重新插入随机位置,而不是像我一样添加它们.

另请注意,此方法效率不高.它必须遍历数组计数($arr)^ 2次.因此,对于1000个值,您将看到1,000,000次迭代.幸运的是,阵列逐渐变小.

点赞