天天看點

java面試題——冒泡排序

public class Show{

public  static void main(String arges[]){

int arr={1,5,3,0,8,4};

int  temp=0;

for(int i=0;i<arr.length-1;i++){  //外部比較

for(int j=0;j<arr.length-1-i;j++){ //内部比較

if(arr[j]>arr[j+1]){

temp=arr[j]; //換位

arr[j]=arr[j+1];

arr[j+1]=temp;

}

}

}

for(int k=0;k<arr.length;k++){

System.out.println(arr[k]);

}

}

}

           

二分查找

package com.singel;

public class Test {

	/**二分查找
	 * 
	 */
	public static void main(String[] args) {
		int a[]={1,4,5,6,7,8};
	BinaryFind bf=new BinaryFind();
	bf.find(0, a.length-1, 1, a);

	}

}class BinaryFind{
	public void find(int left,int right,int val,int a[]){
		if(left<=right){
			int mid=(left+right)/2;
			int m=a[mid];
			if(m<val){
				find(mid+1,right,val,a);
			}else if(m>val){
				find(left,mid-1,val,a);
			}else if(m==val){
				System.out.println("find index");
			}
		}
	}
}
           

package com.data;

public class Show implements IString {
private char strval[]; //字元數組,存放串值
private int curlen;    //目前串的長度
public Show(){
	strval=new char[0];
	curlen=0;
}
public Show(String str){
	char temp[]=str.toCharArray();
	strval=temp;
	curlen=temp.length;
	
}
public Show(char[] val){
	this.strval=new char[val.length];
	for(int i=0;i<val.length;i++){
		this.strval[i]=val[i];
	}
	curlen=val.length;
	
}
	public char charAt(int index) {
		// 查找
		if((index<0)||(index>=this.curlen)){
			throw new StringIndexOutOfBoundsException(index);
		}
		
		return this.strval[index];
	}

	public void clear() {
		// 清空
		this.curlen=0;

	}

	public int compareTo(Show str) {
		// 比較
		int len1=this.curlen;
		int len2=str.curlen;
		int n=Math.min(len1, len2);
		char c1[]=this.strval;
		char c2[]=str.strval;
		int k=0;
		while(k<n){
			char ch1=c1[k];
			char ch2=c2[k];
			if(ch1!=ch2){
				return ch1-ch2;
			}
		}
		return len1-len2;
	}

	public IString concat(IString str) {
		// 連接配接
		return this.insert(this.curlen, str);
	}

	public IString delete(int begin, int end) {
		// 删除
		if(begin<0){
			throw new StringIndexOutOfBoundsException("開始位置不能小于0");
		}
		if(end>this.curlen){
			throw new StringIndexOutOfBoundsException("結束位置不能大于目前串的長度:"+this.curlen);
		}
		if(begin>end){
			throw new StringIndexOutOfBoundsException("開始位置不能大于結束位置");
		}
		for(int i=0;i<this.curlen-end;i++){
			this.strval[begin+i]=this.strval[end+i];
		}
		this.curlen=this.curlen-(end-begin);
		return this;
	}

	public int indexOf(IString str, int begin) {
		// 查找
		return 0;
	}

	public IString insert(int offset, IString str) {
		// 添加
		if((offset<0||(offset>this.curlen))){
			throw new StringIndexOutOfBoundsException("插入位置不合法");
		}
		int len=str.length();
		int newCount=this.curlen+len;
		if(newCount>this.strval.length){
			this.allocate(newCount);
		}
		for(int i=this.curlen-1;i>=offset;i--){
			this.strval[len+i]=this.strval[i];
		}
		for(int i=0;i<len;i++){
			this.strval[offset+i]=str.charAt(i);
		}
		this.curlen=newCount;
		return this;
	}

	public boolean isEmpty() {
		// 是否為空
		return curlen==0;
	}

	public int length() {
		// 長度
		return this.curlen;
	}

	public IString substring(int begin, int end) {
		// 截取
		if(begin<0){
			throw new StringIndexOutOfBoundsException("開始位置不能小于0");
		}
		if(end>this.curlen){
			throw new StringIndexOutOfBoundsException("結束位置不能大于串的目前長度:"+this.curlen);
		}
		if(begin>end){
			throw new StringIndexOutOfBoundsException("開始位置不能大于結束位置");
		}
		if(begin==0&&end==0){
			return this;
		}else{
			char[] buffer=new char[end-begin];
			for(int i=0;i<buffer.length;i++){
				buffer[i]=this.strval[i+begin];
			}
			return new Show(buffer);
		}
		
	}
	public void allocate(int newCapacity) {
		//擴充空間
		char temps[]=this.strval;
		this.strval=new char[newCapacity];
		for(int i=0;i<temps.length;i++){
			this.strval[i]=temps[i];
		}
		
	}

}
           

隊列(鍊式)

package com.jeecms.common.crawler.data;

import java.util.LinkedList;
/**
 * 隊列
 * @author javacoo
 * @since 2011-11-01
 * @param <T>
 */
public class Queue<T> {
	private LinkedList<T> queue = new LinkedList<T>();
	/**
	 * 入隊列
	 * @param t
	 */
	public void enQueue(T t){
		queue.addLast(t);
	}
	/**
	 * 出隊列
	 * @return t
	 */
	public T deQueue(){
		return queue.removeFirst();
	}
	/**
	 * 判斷隊列是否為空
	 * @return
	 */
	public boolean isEmpty(){
		return queue.isEmpty();
	}
	/**
	 * 判斷隊列是否含有t
	 * @param t
	 * @return
	 */
	public boolean contains(T t){
		return queue.contains(t);
	}
	/**
	 * 取得隊列大小
	 * @return
	 */
	public int getSize(){
		return queue.size();
	}

}