Introduction to the implementation of PHP to find the minimum absolute value in ordered arrays

Source: Internet
Author: User
This article mainly introduces the implementation of PHP to find an ordered array of absolute value of the smallest algorithm, simple analysis of the array traversal and binary search algorithm related operation skills, the need for friends can refer to the next

In this paper, we describe the algorithm of PHP implementation to find the smallest absolute value in an ordered array. Share to everyone for your reference, as follows:

Problem:

An ordered array, the value may have negative values, or there may not be, you need to find out the absolute value of the least.

Method 1:

Iterate through the array, find the absolute value minimum, the time complexity O (n), and N is the number of elements.

Method 2:

Binary lookup, because the array is ordered, can be used to find the two points, time complexity O (logn).

Analysis steps:

1. If the first number is positive, it indicates that the entire array has no negative numbers and returns the first number directly

2. If the last number is negative, it indicates that the entire array has no positive number and returns the last digit directly

3. The array element has a positive negative, indicating that the element with the lowest absolute value must be at the positive negative junction, need to find two points to play:

①. If a[mid]<0, because the array is ascending, indicating that the minimum number of absolute values does not appear on the left side of A[mid], while judging the positive and negative of the a[mid+1] element, if it is negative, it needs to be found in the mid right interval, if A[MID-1] is not negative, Then the two numbers are the positive and negative junction points in the array, returning the absolute value of the two numbers is smaller.

②. If a[mid]>0, because the array is ascending, indicating that the minimum number of absolute values does not appear on the right side of A[mid], while judging the positive and negative of the a[mid-1] element, if it is negative, then the two number is the positive and negative junction in the array, and returns the absolute value of the two numbers is smaller if a[ MID-1] is not negative, then you need to look in mid with the left interval.

③. If a[mid] = = 0, then A[mid] is absolutely the smallest element.


function Selectabsminnum (array $arr) {  $start = 0;  $len = count ($arr)-1;  if ($arr [0] > 0) {//positive array    return $arr [0];  }  if ($arr [$len] < 0) {//negative array    return $arr [$len];  }  while ($start < $len) {    $mid = Floor (($start + $len)/2);    if ($arr [$mid] > 0) {      if ($arr [$mid-1] > 0) {        $len = $mid-1;      } else {        return min ($arr [$mid], -$arr [$mid-1]);      }    } ElseIf ($arr [$mid] < 0) {      if ($arr [$mid + 1] < 0) {        $start = $mid + 1;      } else {        return min (-$arr [$mid], $arr [$mid + 1];      }    } else {      return $arr [$mid];}}  $SORTARR = [ -5, -4, -4, -4, 5, 7, 9];echo Selectabsminnum ($SORTARR), Php_eol;

Run Result: 4

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.