Java Array Lookup __java

Source: Internet
Author: User
Import java.util.*;
/**
Interview questions:
Array element Lookup:
1, ordinary array element lookup
2, two-point search
3, Interview questions: The use of two-point search method
4, with Java internal function arrays.binarysearch array binary search, import module imports java.util.*;
*/
Class Arrayelementfind_1
{
public static void Main (string[] args)
{
Int[] arr={234,4,5,75,78};
Int[] arr2={2,4,5,7,32,42,88};
int Index=getindex (arr,4);//normal array element lookup
int Index=halfsearch (arr,75);//binary lookup element
int index2=halfsearch_2 (arr,75);//binary lookup element
int index3=halfsearch_22 (arr2,75);//Insert element and return subscript
int Index4=arrays.binarysearch (arr2,42);
int Index5=arrays.binarysearch (arr2,75);
/* Use the internal function arrays.binarysearch array binary lookup, the return value tells us whether this number is already in the array and if not there should be inserted in that position,
Negative insertion point minus one, equivalent to return-min-1 in halfsearch_22;
*/
System.out.println ("index=" +index);
System.out.println ("index2=" +index2);
System.out.println ("index3=" +index3);
System.out.println ("index4=" +index4);
System.out.println ("index5=" +index5);


}
/*
Array element Lookup
*/


public static int GetIndex (int[] arr,int key)
{
for (int x=0;x<arr.length;x++)
{
if (Arr[x]==key)
return x;
}
return-1;
}
/*
Two-point search method
*/


public static int Halfsearch (int[] arr,int key)
{
int Min,mid,max;
min=0;
Max=arr.length-1;
Mid= (Min+max)/2;
while (Arr[mid]!=key)
{
if (Key>arr[mid])
min=mid+1;
else if (Key<arr[mid])
Max=mid-1;
if (max<min)
return-1;
Mid= (Min+max)/2;
}
return mid;

}
public static int halfsearch_2 (int[] arr,int key)
{
int Min,mid,max;
min=0;
Max=arr.length-1;
Mid= (Min+max)/2;
while (Min<max)
{
Mid= (Min+max) >>1;
if (Key>arr[mid])
min=mid+1;
else if (Key<arr[mid])
Max=mid-1;
Else
return mid;

}
return-1;

}


/*
Interview question: Given an ordered array, if an element is stored in the array, and the array is guaranteed to be ordered, how does the element's stored corner mark get
*/


public static int halfsearch_22 (int[] arr,int key)
{
int Min,mid,max;
min=0;
Max=arr.length-1;
Mid= (Min+max)/2;
while (Min<max)
{
Mid= (Min+max) >>1;
if (Key>arr[mid])
min=mid+1;
else if (Key<arr[mid])
Max=mid-1;
Else
return mid;

}
return min;

}
}

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.