zoukankan      html  css  js  c++  java
  • 二分查找,希尔排序,欧几里得,斐波那契

    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] + "	");
    		}
    	}
           // 递归斐波那契数列	
    	public static int feibo2(int len){
    		if(len == 1 || len == 2){
    			return 1;
    		}
    		return feibo2(len - 1) + feibo2(len - 2);
    	}
    

      

  • 相关阅读:
    Spring+Spring MVC+Hibernate框架搭建实例
    数据结构:串
    为什么java中用枚举实现单例模式会更好
    验证码原理分析及实现
    Servlet监听器——实现在线登录人数统计小例子
    IntelliJ IDEA 远程调试 Tomcat 的方法
    SQL注入的一些技巧分享
    Java中String与byte[]的转换
    JavaSE基础:集合类
    二叉树的非递归遍历
  • 原文地址:https://www.cnblogs.com/SharkChilli/p/11678621.html
Copyright © 2011-2022 走看看