插入排序5

package ch02;

public class InsertSort {
	
	public static void sort(long[] arr) {
		long tmp = 0;
		
		for(int i = 1; i < arr.length; i++) {
			tmp = arr[i];
			int j = i;
			while(j > 0 && arr[j] >= tmp) {
				arr[j] = arr[j - 1];
				j--;
			}
			arr[j] = tmp;
		}
	}
}
package ch02;



public class TestBublleSort {

	public static void main(String[] args) {
		long[] arr = new long[4];
		arr[0] = 34;
		arr[1] = 23;
		arr[2] = 2;
		arr[3] = 1;
		//BublleSort.sort(arr);
		//SelectionSort.sort(arr);
		
		InsertSort.sort(arr);
		
		System.out.print("{");
		for(long num:arr){
			System.out.print(num+"  ");
		}
		System.out.println("}");
		
		
	}
}

  

posted on 2015-02-06 15:26  aicpcode  阅读(126)  评论(0编辑  收藏  举报

导航