查找算法总结
1、顺序查找
package com.namu.search;
public class SeqSearch {
public static int[] array = new int[]{1,2,3,7,10,17};
public static int target = 17;
public static void main(String[] args) {
System.out.println(array[seqSearch1(array,target)]);
}
public static int seqSearch1(int[] array, int target) {
for (int i = 0 ; i < array.length ; i++) {
if (array[i] == target) {
return i;
}
}
return -1;
}
}
2、二分查找
package com.namu.search;
public class BinarySearch {
public static int[] array = new int[]{1,2,3,7,10,17};
public static int target = 17;
public static void main(String[] args) {
System.out.println(array[binarySearch1(array,target)]);
}
public static int binarySearch1(int[] array, int target) {
int low = 0;
int high = array.length-1;
while (low <= high) {
int mid = (low + high) / 2;
if (target < array[mid]) {
high = mid - 1;
}
if (target > array[mid]) {
low = mid + 1;
}
if (target == array[mid]) {
return mid;
}
}
return -1;
}
public static int binarySearch2(int[] array, int low, int high, int target) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (target < array[mid]) return binarySearch2(array, low, mid-1, target);
if (target > array[mid]) return binarySearch2(array, mid+1, high, target);
if (target == array[mid]) return mid;
return -1;
}
}
3、差值查找(自适应的划分,适合平均分布的数组)
package com.namu.search;
public class InsertValueSearch {
public static int[] array = new int[]{1,2,3,7,10,17};
public static int target = 17;
public static void main(String[] args) {
System.out.println(array[insertValueSearch2(array,0,array.length-1,target)]);
}
public static int insertValueSearch1(int[] array, int target) {
int low = 0;
int high = array.length-1;
while (low <= high) {
int mid = low + (target-array[low])/(array[high]-array[low])*(high-low);
if (target < array[mid]) {
high = mid-1;
}
if (target > array[mid]) {
low = mid+1;
}
if (target == array[mid]) {
return mid;
}
}
return -1;
}
public static int insertValueSearch2(int[] array, int low , int high , int target) {
if (low > high) {
return -1;
}
int mid = low + (target-array[low])/(array[high]-array[low])*(high-low);
if (target < array[mid]) return insertValueSearch2(array, low, mid-1, target);
if (target > array[mid]) return insertValueSearch2(array, low+1, high, target);
if (target == array[mid]) return mid;
return -1;
}
}
4、斐波那契查找
package com.namu.search;
import java.util.Arrays;
public class FibonacciSearch {
public static int[] array = new int[]{1, 2, 3, 7, 10, 17};
public static int target = 17;
public static void main(String[] args) {
System.out.println(array[fibonacciSearch1(array,target)]);
}
public static int[] fib() {
int[] f = new int[20];
f[0] = 1;
f[1] = 1;
for (int i = 2 ; i < 20 ; i++) {
f[i] = f[i-1] + f[i-2];
}
return f;
}
public static int fibonacciSearch1(int[] array, int target) {
int low = 0;
int high = array.length-1;
int[] fib = fib();
int f = 0;
while (high > fib[f]-1) {
f++;
}
int[] newArray = Arrays.copyOf(array, fib[f]);
for (int i = high+1 ; i < newArray.length ; i++) {
newArray[i] = array[high];
}
while (low <= high) {
int mid = low + fib[f-1] - 1;
if (target < newArray[mid]) {
high = mid-1;
f-=1;
}
if (target > newArray[mid]) {
low = mid+1;
f-=2;
}
if (target == newArray[mid]) {
if (mid >= high) {
return high;
}else {
return mid;
}
}
}
return -1;
}
}