二分查找,希尔排序,欧几里得,斐波那契

import java.util.*;

public class Ojld{
	public static void main(String[] args){
		//Ojld.run(1997, 615);
		int[] arr = {15,16,11,9,6,17,20,31};
		Integer a = 17;
		int[] realArr = fastSort(arr);
		Integer[] bb = intTransferInteger(realArr);
		System.out.println(Ojld.binarySearch(bb, a));
	}
	
	public static void run(int m, int n){
		while(n != 0){
			int rem = m % n;
			m = n;
			n = rem;
		}
	}
	
	public static Integer[] intTransferInteger(int[] arr){
		int len = arr.length;
		Integer[] it = new Integer[len];
		for(int i=0; i<len; i++){
			it[i] = (Integer)arr[i];
		}
		return it;
	}
	
	public static int[] fastSort(int[] baseArr){
		int N = baseArr.length;
		int h = 1;
		while(h < N / 3){
			h = h * 3 + 1;
		}
		while(h >= 1){
			for(int i = h; i < N; i++){
				for(int j = i; j >= h && baseArr[j] < baseArr[j - h]; j -= h){
					int temp = baseArr[j];
					baseArr[j] = baseArr[j - h];
					baseArr[j - h] = temp;
				}
			}
			h = (int)Math.floor(h / 3);
		}
		return baseArr;
	}
	
	public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType[] arr, AnyType a){
		int low = 0;
		int hight = arr.length - 1;
		int middle = -1;
		while(low <= hight){
			middle = low + (int)Math.ceil((hight - low) / 2);
			System.out.println("low: " + low + ",hight: " + hight + ",middle:" + middle);
			if(arr[middle].compareTo(a) > 0){
				hight = middle - 1;
			}else if(arr[middle].compareTo(a) < 0){
				low = middle + 1;
			}else{
				return middle;
			}
		}
		return -1;
	}
}

  

        // 非递归斐波那契数列	
	public static void feibo1(int len){
		int[] arr = new int[len];
		arr[0] = arr[1] = 1;
		for(int i=2; i<len; i++){
			arr[i] = arr[i-1] + arr[i-2];
		}
		for(int j=0; j<len; j++){
			if(j%5 == 0){
				System.out.println();
			}
			System.out.print(arr[j] + "\t");
		}
	}
       // 递归斐波那契数列	
	public static int feibo2(int len){
		if(len == 1 || len == 2){
			return 1;
		}
		return feibo2(len - 1) + feibo2(len - 2);
	}

  

posted @ 2019-10-15 16:38  SharkChilli  阅读(142)  评论(0编辑  收藏  举报