1. Common Search Algorithm
1 ) order (linear) Find 2 ) binary search / binary search 3 ) Find interpolation 4 ) Find Fibonacci
2. Find
1) linear search
A. Title:
There are a number of columns [ 1 , 43 , 22 - 10 , 0 ], the number of columns is determined whether to include this name, if found, to find the tips, and the index value is given.
B, ideas:
Find one by one
C. Code
com.offcn.search Package; // linear search public class SeqSearch { public static void main (String [] args) { int [] = {ARR . 1 , 43 is , 22 is , - 10 , 0 }; int Result = seqSearch (ARR , 0 ); IF ! (Result = - . 1 ) { . the System OUT .println ( " ! found subscript " + Result); } the else { . the System OUT .println ("未找到"); } } public static int seqSearch(int[] arr,int value){ for(int i = 0;i < arr.length;i++){ if(arr[i] == value){ return i; } } return -1; } }
D. Output:
2) binary search
A. Topic 1:
Please ordered array on a binary search { 1 , 8 , 10 , 89 , 1000 , 1234 }, enter a number to see whether this number of the array, and the subscript determined, if not prompt "this number is not"
B. ideas:
C. Code
package com.offcn.search; //二分查找 public class BinarySearch { public static void main(String[] args){ int[] arr = {1,8,10,89,1000,1234}; int i = binarySearch(arr, 0, arr.length - 1, 1234); if(i == -1){ System.out.println("未找到"); }the else { the System. OUT .println ( " found subscript! " + I); } } public static int binarySearch ( int [] ARR, int left, int right, int findValue) { // If not find, left It may have been mid + 1, out of range IF (left> right) { return - . 1 ; } // intermediate array subscript int mID = (left + right) / 2 ; int midValue =ARR [MID]; // If the element is larger than the intermediate value to find, the recursive right IF (findValue> midValue) { return binarySearch (ARR, MID + . 1 , right, findValue); // less than the intermediate value, left recursive } the else IF (findValue < midValue) { return binarySearch (ARR, left, mid- . 1 , findValue); // exactly intermediate value } the else { return mID; } } }
D. Output:
A. Question 2:
{ 1 , 8 , 10 , 89 , 1000 , 1000 , 1000 , 1000 , 1234 } When an ordered array of a plurality of the same value, all values are how to find?
B. ideas:
C. Code
package com.offcn.search; import java.util.ArrayList; public class BinarySearchList { public static void main(String[] args){ int[] arr = {1,8,10,89,1000,1000,1000,1000,1234}; ArrayList<Integer> list = binarySearchList(arr, 0, arr.length - 1, 1000); System.out.println(list); } public static ArrayList<Integer> binarySearchList(int[] arr,int left,int right,int findValue){ if(left > right){ return new ArrayList<>(); } int mid = (left+right)/2; int midValue= arr[mid]; if(findValue > midValue){ return binarySearchList(arr,mid+1,right,findValue); }else if(findValue < midValue){ return binarySearchList (ARR, left, mid- . 1 , findValue); } the else { // if found, does not return to, a collection means to create the same numerical subscript the ArrayList List = new new the ArrayList (); // left side of the mid scan, to find the same value of the subscript added to the collection int TEMP = mid- . 1 ; the while ( to true ) { IF (TEMP < 0 ! || ARR [TEMP] = findValue) { BREAK ; } List.add (TEMP); the TEMP - = 1 ; } List.add (mid); // to the right of the mid scan, to find the same value of the subscript added to the collection TEMP = mid + . 1 ; the while ( to true ) { IF (TEMP> arr.length- . 1 || ARR [TEMP !] = findValue) { BREAK ; } List.add (TEMP); TEMP + = . 1 ; } return List; } } }
D. Output:
3) Find interpolation
A. Principle:
B. Note:
C. Code
package com.offcn.search; //插值查找 public class InsertValSearch { public static void main(String[] args){ int[] arr = new int[100]; for(int i = 0; i < arr.length;i++){ arr[i] = i+1; } int i = insertValSearch(arr, 0, arr.length-1, 100); System.out.println("下标为"+ I); } public static int insertValSearch ( int [] ARR, int left, int right, int {findVal) . The System OUT .println ( " Interpolation Find 1 " ); // if left> rigth or not the value of findValue correct, returns -1 IF (left> right findVal || <ARR [ 0 ] || findVal> ARR [arr.length- . 1 ]) { return - . 1 ; } // adaptive MID int MID = left + (right - left) * (findVal-arr [ left]) / (arr [right] -arr[left]); int midVal = arr[mid]; if(findVal > midVal){ return insertValSearch(arr,mid+1,right,findVal); }else if(findVal < midVal){ return insertValSearch(arr,left,mid-1,findVal); }else { return mid; } } }
D. Output: