Source: Internet
Author: User

Tags: style blog io ar color os using SP Java

I just collected from the Internet, the following code may be wrong.

Looked at the will Hadoop, and Chuan Hua chat will, he said, they that third prize 8000,; planning to go back to the dorm, inadvertently saw this problem, seemingly simple, in fact, it is more difficult.

You can only do one thing for a while.

There is a sorted array (ascending), there may be positive, negative, or 0 in the array, the minimum number of absolute values of the elements in the array is required, the method cannot be compared in order (complexity needs to be less than O (n)), can be implemented in any language, For example, the array { -20,-13,-4, 6, 77,200}, the smallest absolute value is-4.

The basic idea of the algorithm implementation: find the dividing point of negative and positive numbers, if it is exactly 0 is it, if it is a positive number, and then the left side of the negative value of the absolute comparison, if it is negative, take the absolute value and the right positive comparison. Also consider the case where the array has only positive or negative numbers.

Sequential List Lookup Obviously two points, it seems to Java arrays and collections is not very familiar.

Private Static int getminabsolutevalue (finalint[] source) { int index = Arrays.binarysearch (source, 0); int Insertpos =-1- index; return index >= 0? 0 = = Source.length source.length-1 : Insertpos];}

The algorithm has a bit of a problem, if the array is {-20,-13,-4, 4, 77, 200}, the algorithm can only find a value. When index < 0 o'clock, you should also compare the next insertion point near the two values, the reader can completely solve their own.

So what does the Insertpos mean? Please see the author decomposition.

PackageCom.jaky;ImportJava.util.*; Public classQuest {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method Stubstring[] colors = {"Blue", "Red", "green", "yellow", "orange", "BLACK"}; Arrays.sort (colors); intS2=arrays.binarysearch (colors, "orange"); intS3=arrays.binarysearch (colors, "Violet"); SYSTEM.OUT.PRINTLN (S2+""+S3); }}

the output is: 3-6. Violet does not exist in the colors array, and has been wondering why S3 would be equal to-6 to see the jdk API. * *

* *public static int BinarySearch (byte[] A,byte key) Use the binary search method to search for the specified byte array to obtain the specified value. The array must be sorted (by the sort (byte[]) method before this call is made. If no array is ordered, the result is indeterminate. If the array contains more than one element with the specified value, it is not guaranteed which one is found. Parameters:

A-the array to search for

Key-The value to search for

Return:

Returns the index of the search key if it is contained in an array, otherwise (-(insertion point)-1). The insertion point is defined as the point at which the key is inserted into the array: the first element index greater than this key, or a.length if all elements in the array are smaller than the specified key. Note that this guarantees that if and only if this key is found, the returned value will be >= 0.

All right, finish it, go back to the dorm and sleep.

Walking on the road I remember I forgot to add the reference, hereby declare: From the push Cool (a reptile site, no original link) and CSDN (on the lab computer, unfortunately the history is cleared), here Express thanks. Sleep and sleep ...

Baidu: To find the smallest absolute number