Number of times the elements in the SORT sequence have problems

Source: Internet
Author: User

Number of times the elements in the SORT sequence have problems
Here we use the half-fold method to give two ideas about the number of elements in the SORT series: 1. Find an index position of the elements in the array, because the array is ordered, you can find all the positions where the elements appear. 2. You can use the half-fold method to find the maximum and minimum index positions of the elements, and then subtract the two index locations and add one, the number of times an element appears. In method 1, the complexity of the method for searching the element location is O (logn), and the complexity of the left and right traversal elements is O (n). Therefore, the total complexity is O (logn) + k, k indicates the number of times the element appears. This method is not suitable when n is small and k is large. In method 2, the time complexity is O (logn), but two searches are performed. If n is large, it is not small. Therefore, this method is not suitable when n is large and k hours. Method 1 PHP code implementation

/** @ Function: returns the position where elements appear in an ordered array. * @ param: The ordered array. * @ param: the element to be searched. * @ param: true: the maximum position, false: Find the minimum position * @ return: returns the index of the element's position. No zero */function arr_index_value ($ arr, $ num, $ flag) is returned. {$ left = 0; $ right = count ($ arr)-1; while ($ left <= $ right) {$ mid = (int) ($ left + $ right)/2 ); if ($ arr [$ mid]> $ num) {// note that in order to change the value of $ mid along with the while loop in all circumstances, $ mid must be added to $ right = $ mid-1;} else if ($ arr [$ mid] <$ num) {// pay attention In this case, the $ mid value can change in a certain direction with the while loop. $ mid must be reduced to $ left = $ mid + 1;} else {// If $ flag is true, obtain the maximum index location if (! $ Flag) {if ($ mid = 0 | $ arr [$ mid-1]! = $ Num) return $ mid; $ right = $ mid-1;} // If $ flag is false, obtain the minimum index location else {if ($ mid = $ right | $ arr [$ mid + 1]! = $ Num) return $ mid; $ left = $ mid + 1 ;}}}}

 

PHP code implementation in Method 2:
/** @ Function: returns the number of times an element in an ordered array appears. * Idea: Find the maximum index location and the minimum index location of the element in the array respectively. * then subtract the two and add one to the number of occurrences. * @ Param: ordered array * @ param: element to be searched * @ return: number of times an element appears. If it does not exist, zero */function arr_count_values2 ($ arr, $ num) {// maximum index location-Minimum index location + 1 $ max = arr_index_value ($ arr, $ num, true); $ min = arr_index_value ($ arr, $ num, false); if ($ max = 0) return 0; return $ max-$ min + 1;}/** @ function: semi-query the position where elements in an ordered array appear * @ param: an ordered array * @ param: the element to be searched * @ param: true: Find the maximum position, false: Find the minimum position * @ return: returns the index of the element's position. No zero */function arr _ is returned _ Index_value ($ arr, $ num, $ flag) {$ left = 0; $ right = count ($ arr)-1; while ($ left <= $ right) {$ mid = (int) ($ left + $ right)/2); if ($ arr [$ mid]> $ num) {// note that in order to change the $ mid value along with the while loop in all circumstances, $ mid must be added to $ right = $ mid-1 ;} else if ($ arr [$ mid] <$ num) {// note that in order to change the $ mid value along with the while loop in all circumstances, $ mid must be reduced to $ left = $ mid + 1;} else {// if $ flag is false, obtain the minimum index location if (! $ Flag) {if ($ mid = 0 | $ arr [$ mid-1]! = $ Num) return $ mid; $ right = $ mid-1;} // If $ flag is true, obtain the maximum index location else {if ($ mid = $ right | $ arr [$ mid + 1]! = $ Num) return $ mid; $ left = $ mid + 1 ;}}// test $ a = array (1, 2, 2, 2, 3, 3, 5, 8); print arr_count_values2 ($ a, 2 );

 

We can further optimize method 2 to reduce a half-fold process and further reduce the time complexity. PHP implementation code in method 3:
/** @ Function: returns the number of times an element in an ordered array appears. * Idea: Find the maximum index location and the minimum index location of the element in the array respectively. * then subtract the two and add one to the number of occurrences. * @ Param: ordered array * @ param: element to be searched * @ return: number of times an element appears. If it does not exist, zero */function arr_count_values3 ($ arr, $ num) {$ left = 0; $ right = count ($ arr)-1; // returns the element position by half. while ($ left <= $ right) {$ mid = (int) ($ left + $ right)/2); if ($ arr [$ mid]> $ num) {// note that in order to change the $ mid value along with the while loop in all circumstances, $ mid must be added to $ right = $ mid-1 ;} else if ($ arr [$ mid] <$ num) {// note that in order to change the $ mid value along with the while loop in all circumstances, $ mid must be reduced to $ left = $ mi D + 1 ;}else {break ;}if ($ left> $ right) return 0; $ mid_t = $ mid; $ left_t = $ left; $ right_t = $ right; // obtain the minimum index position using the half-fold method. while ($ left <= $ right) {$ mid = (int) ($ left + $ right)/2 ); if ($ mid = 0 | $ arr [$ mid-1]! = $ Num) {$ min = $ mid; break;} $ right = $ mid-1;} // obtain the maximum index position by half-means 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 ;} // Number of element occurrences = maximum index location-Minimum index location + 1 $ max = arr_index_value ($ arr, $ num, true); $ min = arr_index_value ($ arr, $ num, false); return $ max-$ min + 1 ;}

 

It can be seen that method 3 is the optimal solution, suitable for various situations, especially when the array is large and the number of elements appears is large, it can better reflect its advantages.

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.