PHP Array Search algorithm

Source: Internet
Author: User
Suppose the array has 10,000 elements, the key value is a unordered positive integer less than 1000000, and is not contiguous, as follows

$arr =array (1=> ' a ',20=> ' ad ',5002=> ' ss ',190023=> ' SD ',248=> ' ff ',76=> ' sddd ' ...);

Now you want to get the elements in the array $arr with a key value greater than 500 and less than 600, do you have a more efficient algorithm without using foreach to loop through it all over again?

Reply content:

Suppose the array has 10,000 elements, the key value is a unordered positive integer less than 1000000, and is not contiguous, as follows

$arr =array (1=> ' a ',20=> ' ad ',5002=> ' ss ',190023=> ' SD ',248=> ' ff ',76=> ' sddd ' ...);

Now you want to get the elements in the array $arr with a key value greater than 500 and less than 600, do you have a more efficient algorithm without using foreach to loop through it all over again?

$res = Array ();
for (i=501;i<600;i++) {
if (!isset ($arr [$i])) continue;
$res [] = $arr [$i];
}

@ Landlord
PHP built-in sorting sort is fast sort, time complexity is O (NLOGN), then you use a binary choice or something, you can pick out.
Overall time complexity of O (NLOGN)

And if the traversal, each time the complexity of O (n), to check the number of I-segment, time complexity is O (i*n), I relatively large, is almost O (n^2), but the actual situation should I far less than N, time complexity of about O (n)

In addition, if you sort first, you need a copy, the memory consumption will be higher.
So I have to weigh it.

@ Zhou Xiang, I tested it, Array_walk () takes longer than foreach, and calls the same custom function.

walk_test.php
 
   foreach_test.php
 
  

root@debian:~/coding/php/test# Time PHP foreach_test.php

Real 0m2.286s
User 0m1.088s
SYS 0m1.156s
root@debian:~/coding/php/test# Time PHP walk_test.php

Real 0m2.653s
User 0m2.352s
SYS 0m0.276s
root@debian:~/coding/php/test# Time PHP walk_test.php

Real 0m2.689s
User 0m1.864s
SYS 0m0.804s
root@debian:~/coding/php/test# Time PHP walk_test.php

Real 0m2.700s
User 0m2.460s
SYS 0m0.216s
root@debian:~/coding/php/test# Time PHP foreach_test.php

Real 0m2.227s
User 0m2.016s
SYS 0m0.188s
root@debian:~/coding/php/test# Time PHP foreach_test.php

Real 0m2.276s
User 0m2.056s
SYS 0m0.200s

I don't know why I'm doing this.


  
    600 || $v < 500)        {            continue;        }        yield $arr[$v];    }}//生成测试数组$arr = [];for($i = 0; $i < 10000; $i++){        $k =  mt_rand(1, 1000000);        $arr[$k] = 'cdddsss';}//获取数组key,对key排序,使用生成器,取出key值在500-600之间的数据$st = microtime(true);$key = array_keys($arr);sort($key);$result = array();foreach(getItem($key ,$arr) as $v){    $result[] = $v;}echo (memory_get_usage() / 1024 /1024) . "M\n";echo microtime(true) - $st . "\n";echo "原数组个数:" . count($arr) . "\n";echo "结果数组个数" . count($result) . "\n";

According to the topic, the array length is fixed, 1000, so Array_key gets the key value sort sort
Then the generator gets the demand interval value
Advantages: Low built-in, stable and reliable performance
Requires PHP5.5 version
PS: All the above answers are not carefully read the title of the main topic, he will be based on key to return data, the number of keys is fixed 10000, so sort(array_keys($arr)) you can

If you want to discuss efficiency, it is not recommended to create an array with 10,000 elements.

Use

Array_walk ()

You can not go to the PHP layer to foreach, but the implementation of Array_walk is actually traversing the entire hashtable.

Unless it's orderly, how do you promise not to miss the data without going through it?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.