The number of occurrences of elements in a sorted sequence

Source: Internet
Author: User

About the "number of elements in a sorted sequence", here are two ways of thinking using binary method:

1, find the element in the array of an index position, because the array is ordered, all in this position around the move, you can find out all its occurrence position;

2, through the binary method to find the maximum index position of the element and the minimum index position, and then subtract two index positions plus one, that is, the number of occurrences of the element.

Method 1 finds the position of the element in the complexity of O (LOGN), the complexity of the left and right traversal element is O (n), so the total complexity is O (logn) + K, where k is the number of occurrences of the element.

This method is not suitable when n is small and k is large.

The time complexity in Method 2 is O (Logn), but two lookups are performed, and if n is large it is also a small open. So when n large, k hours, not quite suitable for this method.

  PHP code Implementation of Method 1

/** @function: Binary find the position of an element in a sequential array * @param: Array in order * @param: the element to find * @param: True to find the maximum position, false to find the smallest position * @return: Returns the position index of the element, No return 0*/functionArr_index_value ($arr,$num,$flag){    $left= 0; $right=Count($arr)-1;  while($left<=$right){        $mid= (int) (($left+$right)/2); if($arr[$mid] >$num){            //Note In order for the $mid value to change in a certain direction with the while loop in all cases, $mid must pay a $right            $right=$mid-1; }Else if($arr[$mid] <$num){            //Note In order for the $mid value to change in some direction with the while loop in all cases, $mid must be reduced by one to $left            $left=$mid+ 1; }Else{            //If $flag is true, get the maximum index position            if(!$flag){                if($mid= = 0 | |$arr[$mid-1]! =$num)return $mid; $right=$mid-1; }            //if $flag is false, get the minimum index position            Else{                if($mid==$right||$arr[$mid+1]! =$num)return $mid; $left=$mid+ 1; }                        }    }}

  Method 2 of the PHP code implementation :

/** @function: Binary finds the number of occurrences of an element in an ordered array. * Idea: Find the maximum index position of the element in the array and the minimum index position, * then subtract two, plus one is the number of occurrences. * @param: Ordered array in order * @param: the element to be searched * @return: The number of occurrences of the element, or 0 if it does not exist*/functionArr_count_values2 ($arr,$num){    //Maximum index position-Minimum index position + 1    $max= Arr_index_value ($arr,$num,true); $min=arr_index_value ($arr,$num,false); if($max= = 0)return0; return $max-$min+ 1;}/** @function: Binary find the position of an element in a sequential array * @param: Array in order * @param: the element to find * @param: True to find the maximum position, false to find the smallest position * @return: Returns the position index of the element, No return 0*/functionArr_index_value ($arr,$num,$flag){    $left= 0; $right=Count($arr)-1;  while($left<=$right){        $mid= (int) (($left+$right)/2); if($arr[$mid] >$num){            //Note In order for the $mid value to change in a certain direction with the while loop in all cases, $mid must pay a $right            $right=$mid-1; }Else if($arr[$mid] <$num){            //Note In order for the $mid value to change in some direction with the while loop in all cases, $mid must be reduced by one to $left            $left=$mid+ 1; }Else{            //if $flag is false, get the minimum index position            if(!$flag){                if($mid= = 0 | |$arr[$mid-1]! =$num)return $mid; $right=$mid-1; }            //If $flag is true, get the maximum index position            Else{                if($mid==$right||$arr[$mid+1]! =$num)return $mid; $left=$mid+ 1; }                        }    }}//Test$a=Array(1, 2, 2, 2, 3, 3, 5, 8);PrintArr_count_values2 ($a, 2);

  

We can further optimize Method 2, reduce the binary process, and further reduce the complexity of time.

 PHP implementation code for Method 3:

    

/** @function: Binary finds the number of occurrences of an element in an ordered array. * Idea: Find the maximum index position of the element in the array and the minimum index position, * then subtract two, plus one is the number of occurrences. * @param: Ordered array in order * @param: the element to be searched * @return: The number of occurrences of the element, or 0 if it does not exist*/functionArr_count_values3 ($arr,$num){    $left= 0; $right=Count($arr)-1; //The binary method obtains the element one position     while($left<=$right){        $mid= (int) (($left+$right)/2); if($arr[$mid] >$num){            //Note In order for the $mid value to change in a certain direction with the while loop in all cases, $mid must pay a $right            $right=$mid-1; }Else if($arr[$mid] <$num){            //Note In order for the $mid value to change in some direction with the while loop in all cases, $mid must be reduced by one to $left            $left=$mid+ 1; }Else{             Break; }    }        if($left>$right)return0; $mid _t=$mid; $left _t=$left; $right _t=$right; //binary method to get the minimum index position     while($left<=$right){        $mid= (int) (($left+$right)/2); if($mid= = 0 | |$arr[$mid-1]! =$num){            $min=$mid;  Break; }        $right=$mid-1; }        //binary method to obtain the maximum index position     while($left _t<=$right _t){        $mid _t= (int) (($left _t+$right _t)/2); if($mid _t==$right _t||$arr[$mid _t+1]! =$num){            $max=$mid _t;  Break; }        $left _t=$mid _t+ 1; }        //element occurrences = Maximum index position-Minimum index position + 1    $max= Arr_index_value ($arr,$num,true); $min=arr_index_value ($arr,$num,false); return $max-$min+ 1;}

Can be seen, Method 3 is the optimal solution, suitable for all kinds of situations, especially when the array is large, the number of elements appear more often, more to reflect its advantages ~

Number of occurrences of elements in sorted column

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.