Finding the absolute value of the absolute minimum in a non-descending sequence

Source: Internet
Author: User

given an ordered integer sequence (non-descending order), it may contain negative numbers to find the element with the smallest absolute value, such as the given sequence-5,-3,-1, 2, 8-returns 1.
Analysis: Because the given sequence is ordered, and this is the search problem, so first think of binary search method, but this dichotomy is slightly more troublesome than the ordinary dichotomy, can be divided into the following situations
If all the numbers in the given sequence are positive, then the first element of the array is the result.
If all the numbers in the given sequence are negative, then the last element of the array is the result.
If the given sequence has both positive and negative values, the minimum value of the absolute number must be at the boundary of the positive and negative numbers.
Why? Because for a negative sequence, the number on the right is smaller than the number on the left, such as-5,-3,-1, and for an entire number, the left is a small absolute value, such as the above 2, 8, the idea for a binary search, you can first judge the middle element and the two elements of the symbol, and then determine the search Gradually narrow the search interval until only two elements are left.
Set a function individually to determine whether the symbols of the two integers are the same

1 /*2 given an ordered integer sequence (non-descending order), it may contain negative numbers to find the element with the smallest absolute value, such as the given sequence-5,-3,-1, 2, 8-returns 1. 3 Analysis: Because the given sequence is ordered, and this is the search problem, so first think of binary search method, but this dichotomy is slightly more troublesome than the ordinary dichotomy, can be divided into the following situations4 If all the numbers in the given sequence are positive, then the first element of the array is the result. 5 If all the numbers in the given sequence are negative, then the last element of the array is the result. 6 if the given sequence has both positive and negative values, the minimum value of the absolute number must be at the boundary of the positive and negative numbers. 7 */8#include <iostream>9#include <cmath>Ten  One using namespacestd; A  - BOOLSamesign (intMintN) - { the     if((m>> to) = = (n>> to)) -         return true; -     Else -         return false; + } -  + //find the minimum number of absolute values in a non-descending certificate sequence A intGetminnumberabsolutevalue (int*arr,intN) at { -     if(n = =1) -         returnArr[n]; -     if(Samesign (arr[0], arr[n-1])) -         returnarr[0] >=0? arr[0]: ABS (arr[n-1]); -  in     //Two-point search -     intMid, Low =0, high = n1; to      while(Low <High ) +     { -         if(Low +1==High ) the             returnABS (Arr[low]) < ABS (Arr[high])?ABS (Arr[low]): ABS (Arr[high]); *Mid = (low + high) >>1; $         if(Samesign (Arr[low], Arr[mid]))Panax Notoginseng         { -Low = mid;//the demarcation point is at mid. the             Continue; +         } A         if(Samesign (Arr[high], Arr[mid])) the         { +High =mid; -             Continue; $         } $     } -     returnABS (Arr[low]); - } the  - intMain ()Wuyi { the     intarr[5] = {-5, -3, -1,2,7}; -  Wucout <<"the absolute value of the absolute minimum in arr is:"<< Getminnumberabsolutevalue (arr,5) <<Endl; -     return 0; About}

Finding the absolute value of the absolute minimum in a non-descending sequence

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.