递归搜索字符数组中的最大字符递归搜索字符数组中的最大字符:
/***
*/
package com.ligaogang.sockettest;
import java.util.Calendar;
/**
* @author Administrator
*
*/
public class maxChar {
/**
* @param args
*/
public static char max(char[] a,int position,int length,char max){
char mmax=max;
if(a[position]>mmax)mmax=a[position];
if(position<length-1) return max(a,++position,length,mmax);
else return mmax;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
long firsttime=Calendar.getInstance().get(Calendar.MILLISECOND);
char[] exam={'a','t','h','y','g','f'};
int length=exam.length;
char max=exam[0];
System.out.println("the max char is:"+max(exam,1,length,max));
long lasttime=Calendar.getInstance().get(Calendar.MILLISECOND);
System.out.println("digui using time :"+(lasttime-firsttime)+" millisecond");
firsttime=Calendar.getInstance().get(Calendar.MILLISECOND);
for(int i=1;i<length;i++){
if(exam[i]>max)max=exam[i];
}
System.out.println("the max char is:"+max);
lasttime=Calendar.getInstance().get(Calendar.MILLISECOND);
System.out.println("for using time:"+(lasttime-firsttime)+" millisecond");
}
}
//个人认为用一次for循环搜索时间复杂度和空间复杂度小,递归反复进栈出栈空间复杂度大。