9.1最大最小值

问1:得到最大或最小的话,比较次数?

             n-1

问2:同时的到最大最小,比较次数?

           奇数:3⌊n/2⌋ 偶数:1+3(n-2)/2

  附上代码:

  

public static void main(String[] args) {
		int[] a = {62,36,75,135,134,34,63,63,208};
		
		System.out.println(minMax(a));
	}
	//同时取最大最小
	public static String  minMax(int[] a){
		int max = a[0];
		int min = a[0];
		for(int i=0;i<a.length/2;i++){
			int max2 = a[i];
			int min2 = a[i];
			if(a[i] < a[a.length/2+i]){
				max2 = a[a.length/2+i];
			}else {
				min2 = a[a.length/2+i];
			}
			if(max2 > max){
				max = max2;
			}
			if(min2 < min){
				min = min2;
			}
		}
		if(a.length%2==1){
			if(a[a.length-1]>max){
				max = a[a.length-1];
			}
			if(a[a.length-1]<min){
				min = a[a.length-1];
			}
		}
		String sys  = "max="+max+";min="+min;
		return sys; 
	}


问3:求一个数组最小和第二小的算法,最坏情况下,比较次数为n+⌈lgn⌉-2

  

public class MinMax {
	
	static int[] a = {62,36,75,135,134,34,63,63,208};
	
	static int[][] map = new int[a.length][a.length];
	
	static int minIndex = 0;
	
	static int minIndex2 = 0;
	
	public static void main(String[] args) {
		
		findMin1(new int[a.length],new int[a.length]);
		System.out.println("min1="+a[minIndex]+";min2="+a[minIndex2]);
	}
	
	public static void findMin1(int[] index,int[] bucketIndex){
		if(index.length == 1){
			minIndex = a[index[0]]<a[a.length-1]?index[0]:a.length-1;
			return;
		}
		int halfLenth = index.length/2;
		int[] b = new int[halfLenth];
		for(int i=0;i<halfLenth;i++){
			int indexA = index[i]; 
			if(a[indexA]<a[i+halfLenth]){
				b[i] = indexA;
			}else {
				b[i] = indexA+halfLenth;
			}
			bucketIndex[indexA] ++;
			bucketIndex[indexA+halfLenth]++;
			map[indexA][bucketIndex[indexA]]=indexA+halfLenth;
			map[indexA+halfLenth][bucketIndex[indexA+halfLenth]]=indexA;
		}
		findMin1(b,bucketIndex);	
	}
	
	static void findMin2(){
		
		int[] com = map[minIndex]; 
		minIndex2 = com[0];
		for(int i=1;i<com.length;i++){
			if(a[com[i]]<a[minIndex]){
				minIndex2 = com[i];
			}
		}
	}    


问4:找出数组里第i小的数

public static void main(String[] args) {
		int[] a = {62,36,75,135,134,34,63,63,208};
		for(int i=1;i<=a.length;i++){
		    System.out.println("第"+i+"小的数:"+minWhich(a,0,a.length-1, i));
		}
		
	}
	static int minWhich(int[] a,int p,int r,int i){
		if(p==r){
			return a[r];
		}
		int k = randomSelect(a, p, r);//
		int index = k-p+1;//子数组中的排名
		if(i==index){
			return a[k];
		}else if( i<index){
			return minWhich(a, p,k-1,i);
		}else {
			return minWhich(a,k+1, r, i-index);
		}
	}
    static int randomSelect(int[] a,int p,int r){
    	int randomInt = p+new Random().nextInt(r+1-p);
    	int i = p-1;
    	int j = p; 
    	swap(a, randomInt, r);
    	while(j<r){
    		if(a[j]<a[r]){
    			i++;
    			swap(a, i, j);
    		}
    		j++;
    	}
    	swap(a, i+1, r);
    	return i+1;
    }
    
    static void swap(int[] a,int i,int j){
    	if(i==j)return;
    	a[i] = a[i]^a[j];
		a[j] = a[i]^a[j];
		a[i] = a[i]^a[j];
    	/*a[i] = a[i]+a[j];
		a[j] = a[i]-a[j];
		a[i] = a[i]-a[j];*/
    }


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值