1.4ソートの挿入

1299 ワード

package array;
/**
 * *  
 * @author  
 * 1:14:56 AM
 */
public class InsertSort {
	private long[] a;
	private int nElement;
	long time=0;
	/**
	 *  
	 * 
	 * @param size
	 */
	public InsertSort(int size) {
		a = new long[size];
		nElement=size;
	}
	/**
	 *  
	 * 
	 * @param point
	 * @param value
	 */
	public void setElem(int point, long value) {
		a[point] = value;
	}
	/**
	 *  
	 * @param index
	 * @return long
	 */
	public long getElem(int index){
		return a[index];
	}
	/** */
	public void display(){
		for (int i = 0; i<a.length; i++) {
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	public  void  displayRunTime() {
		System.out.println("Run Time:"+time);
	}
	/** */
	public void insertSort(){
		int in,point;
		for (point=1;point<nElement;point++) {
			in=point;
			long temp=a[point];
			while(in>0&&a[in-1]>=temp){
				a[in]=a[in-1];
				in--;
				time++;
			}
			a[in]=temp;
		}
	}
}