Find the element with the smallest absolute value in the array

Source: Internet
Author: User

Given an ordered array of a (from small to large), the data in the array has a positive negative, identifying the element with the smallest absolute value in the array. The first natural thing to do is to iterate through each element of the array in turn to find the element with the smallest absolute value. This is the simplest method, but it doesn't use the array ordering feature, so let's see if there's a better way. The topic asks for an element to be found in the array, and this array is ordered, so you can think of the binary method to handle it.

First, let's look at the case where the elements in the array are all positive or negative all:

    • If a[0]>0, then all elements in the array are positive, then a[0] is the element with the smallest absolute value
    • If a[len-1]<0, then all elements in the array are negative, then a[len-1] is the element with the smallest absolute value

When an element in an array has a positive negative, the element with the smallest absolute value is at the junction of the positive and negative numbers, which is only required to compare the size of the absolute value of the adjacent two digits of the junction point, and return the absolute value to be small. Set A[mid] as the middle element of an array, you can find it in the following steps:

    • If a[mid]<0, because the array is ascending, the minimum number of absolute values does not appear on the left side of A[mid] and needs to be found in mid-right intervals
    • 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], where both the positive and negative of the a[mid-1] element on the left side of the a[mid] are judged, if negative, then the two numbers are the positive and negative junctions in the array, Returns the absolute value of these two numbers is smaller, if a[mid-1] is not negative, then you need to find the mid-left interval
    • If a[mid] = = 0, then A[mid] is absolutely the smallest element
    • Interval range changes, the mid in the interval is re-assigned, repeat the above steps

Write the code according to the steps above:

1 intFindminabs (intA[],intlen)2 {3     //if the first element of a array is >0, the number after a[0] is >0, and is larger than a[0]4     if(a[0]>0)5         returna[0];6     //if the last element of a array is <0, the number before a[len-1] is <0, and is smaller than a[len-1], so the minimum abs (a[len-1])7     Else if(a[len-1]<0)8         returna[len-1];9 Ten     intleft=0, right=len-1, mid= (left+right)/2; One     intI=0; A  -     //if a[0]<0,a[len-1]>0, then the absolute minimum number must appear at the positive and negative junction points -      while(true) the     { -cout<<"mid="<<mid<<", a[mid]="<<a[mid]<<", left="<<left<<", right="<<right<<Endl; -         if(a[mid]<0) -         { +left = mid+1; -         } +         Else if(a[mid]>0) A         { at             //if A[mid] and a[mid-1] are one minus, so just determine the abs size of both of them -             if(a[mid]*a[mid-1] <=0) -                 return-a[mid-1] < A[mid]? a[mid-1]:a[mid]; -right = mid-1; -         } -         Else in             returnA[mid]; -  toMid = (left+right)/2; +     } -}

Find the element with the smallest absolute value in the array

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.